PageRenderTime 117ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Modules/audioop.c

http://unladen-swallow.googlecode.com/
C | 1655 lines | 1271 code | 195 blank | 189 comment | 682 complexity | a9ee38966c9e210f6dd9a37ea28176aa MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  1. /* audioopmodule - Module to detect peak values in arrays */
  2. #include "Python.h"
  3. #if SIZEOF_INT == 4
  4. typedef int Py_Int32;
  5. typedef unsigned int Py_UInt32;
  6. #else
  7. #if SIZEOF_LONG == 4
  8. typedef long Py_Int32;
  9. typedef unsigned long Py_UInt32;
  10. #else
  11. #error "No 4-byte integral type"
  12. #endif
  13. #endif
  14. typedef short PyInt16;
  15. #if defined(__CHAR_UNSIGNED__)
  16. #if defined(signed)
  17. /* This module currently does not work on systems where only unsigned
  18. characters are available. Take it out of Setup. Sorry. */
  19. #endif
  20. #endif
  21. /* Code shamelessly stolen from sox, 12.17.7, g711.c
  22. ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
  23. /* From g711.c:
  24. *
  25. * December 30, 1994:
  26. * Functions linear2alaw, linear2ulaw have been updated to correctly
  27. * convert unquantized 16 bit values.
  28. * Tables for direct u- to A-law and A- to u-law conversions have been
  29. * corrected.
  30. * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
  31. * bli@cpk.auc.dk
  32. *
  33. */
  34. #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
  35. #define CLIP 32635
  36. #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
  37. #define QUANT_MASK (0xf) /* Quantization field mask. */
  38. #define SEG_SHIFT (4) /* Left shift for segment number. */
  39. #define SEG_MASK (0x70) /* Segment field mask. */
  40. static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
  41. 0x1FF, 0x3FF, 0x7FF, 0xFFF};
  42. static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
  43. 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
  44. static PyInt16
  45. search(PyInt16 val, PyInt16 *table, int size)
  46. {
  47. int i;
  48. for (i = 0; i < size; i++) {
  49. if (val <= *table++)
  50. return (i);
  51. }
  52. return (size);
  53. }
  54. #define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
  55. #define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
  56. static PyInt16 _st_ulaw2linear16[256] = {
  57. -32124, -31100, -30076, -29052, -28028, -27004, -25980,
  58. -24956, -23932, -22908, -21884, -20860, -19836, -18812,
  59. -17788, -16764, -15996, -15484, -14972, -14460, -13948,
  60. -13436, -12924, -12412, -11900, -11388, -10876, -10364,
  61. -9852, -9340, -8828, -8316, -7932, -7676, -7420,
  62. -7164, -6908, -6652, -6396, -6140, -5884, -5628,
  63. -5372, -5116, -4860, -4604, -4348, -4092, -3900,
  64. -3772, -3644, -3516, -3388, -3260, -3132, -3004,
  65. -2876, -2748, -2620, -2492, -2364, -2236, -2108,
  66. -1980, -1884, -1820, -1756, -1692, -1628, -1564,
  67. -1500, -1436, -1372, -1308, -1244, -1180, -1116,
  68. -1052, -988, -924, -876, -844, -812, -780,
  69. -748, -716, -684, -652, -620, -588, -556,
  70. -524, -492, -460, -428, -396, -372, -356,
  71. -340, -324, -308, -292, -276, -260, -244,
  72. -228, -212, -196, -180, -164, -148, -132,
  73. -120, -112, -104, -96, -88, -80, -72,
  74. -64, -56, -48, -40, -32, -24, -16,
  75. -8, 0, 32124, 31100, 30076, 29052, 28028,
  76. 27004, 25980, 24956, 23932, 22908, 21884, 20860,
  77. 19836, 18812, 17788, 16764, 15996, 15484, 14972,
  78. 14460, 13948, 13436, 12924, 12412, 11900, 11388,
  79. 10876, 10364, 9852, 9340, 8828, 8316, 7932,
  80. 7676, 7420, 7164, 6908, 6652, 6396, 6140,
  81. 5884, 5628, 5372, 5116, 4860, 4604, 4348,
  82. 4092, 3900, 3772, 3644, 3516, 3388, 3260,
  83. 3132, 3004, 2876, 2748, 2620, 2492, 2364,
  84. 2236, 2108, 1980, 1884, 1820, 1756, 1692,
  85. 1628, 1564, 1500, 1436, 1372, 1308, 1244,
  86. 1180, 1116, 1052, 988, 924, 876, 844,
  87. 812, 780, 748, 716, 684, 652, 620,
  88. 588, 556, 524, 492, 460, 428, 396,
  89. 372, 356, 340, 324, 308, 292, 276,
  90. 260, 244, 228, 212, 196, 180, 164,
  91. 148, 132, 120, 112, 104, 96, 88,
  92. 80, 72, 64, 56, 48, 40, 32,
  93. 24, 16, 8, 0
  94. };
  95. /*
  96. * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
  97. * stored in a unsigned char. This function should only be called with
  98. * the data shifted such that it only contains information in the lower
  99. * 14-bits.
  100. *
  101. * In order to simplify the encoding process, the original linear magnitude
  102. * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
  103. * (33 - 8191). The result can be seen in the following encoding table:
  104. *
  105. * Biased Linear Input Code Compressed Code
  106. * ------------------------ ---------------
  107. * 00000001wxyza 000wxyz
  108. * 0000001wxyzab 001wxyz
  109. * 000001wxyzabc 010wxyz
  110. * 00001wxyzabcd 011wxyz
  111. * 0001wxyzabcde 100wxyz
  112. * 001wxyzabcdef 101wxyz
  113. * 01wxyzabcdefg 110wxyz
  114. * 1wxyzabcdefgh 111wxyz
  115. *
  116. * Each biased linear code has a leading 1 which identifies the segment
  117. * number. The value of the segment number is equal to 7 minus the number
  118. * of leading 0's. The quantization interval is directly available as the
  119. * four bits wxyz. * The trailing bits (a - h) are ignored.
  120. *
  121. * Ordinarily the complement of the resulting code word is used for
  122. * transmission, and so the code word is complemented before it is returned.
  123. *
  124. * For further information see John C. Bellamy's Digital Telephony, 1982,
  125. * John Wiley & Sons, pps 98-111 and 472-476.
  126. */
  127. static unsigned char
  128. st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
  129. {
  130. PyInt16 mask;
  131. PyInt16 seg;
  132. unsigned char uval;
  133. /* The original sox code does this in the calling function, not here */
  134. pcm_val = pcm_val >> 2;
  135. /* u-law inverts all bits */
  136. /* Get the sign and the magnitude of the value. */
  137. if (pcm_val < 0) {
  138. pcm_val = -pcm_val;
  139. mask = 0x7F;
  140. } else {
  141. mask = 0xFF;
  142. }
  143. if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
  144. pcm_val += (BIAS >> 2);
  145. /* Convert the scaled magnitude to segment number. */
  146. seg = search(pcm_val, seg_uend, 8);
  147. /*
  148. * Combine the sign, segment, quantization bits;
  149. * and complement the code word.
  150. */
  151. if (seg >= 8) /* out of range, return maximum value. */
  152. return (unsigned char) (0x7F ^ mask);
  153. else {
  154. uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
  155. return (uval ^ mask);
  156. }
  157. }
  158. static PyInt16 _st_alaw2linear16[256] = {
  159. -5504, -5248, -6016, -5760, -4480, -4224, -4992,
  160. -4736, -7552, -7296, -8064, -7808, -6528, -6272,
  161. -7040, -6784, -2752, -2624, -3008, -2880, -2240,
  162. -2112, -2496, -2368, -3776, -3648, -4032, -3904,
  163. -3264, -3136, -3520, -3392, -22016, -20992, -24064,
  164. -23040, -17920, -16896, -19968, -18944, -30208, -29184,
  165. -32256, -31232, -26112, -25088, -28160, -27136, -11008,
  166. -10496, -12032, -11520, -8960, -8448, -9984, -9472,
  167. -15104, -14592, -16128, -15616, -13056, -12544, -14080,
  168. -13568, -344, -328, -376, -360, -280, -264,
  169. -312, -296, -472, -456, -504, -488, -408,
  170. -392, -440, -424, -88, -72, -120, -104,
  171. -24, -8, -56, -40, -216, -200, -248,
  172. -232, -152, -136, -184, -168, -1376, -1312,
  173. -1504, -1440, -1120, -1056, -1248, -1184, -1888,
  174. -1824, -2016, -1952, -1632, -1568, -1760, -1696,
  175. -688, -656, -752, -720, -560, -528, -624,
  176. -592, -944, -912, -1008, -976, -816, -784,
  177. -880, -848, 5504, 5248, 6016, 5760, 4480,
  178. 4224, 4992, 4736, 7552, 7296, 8064, 7808,
  179. 6528, 6272, 7040, 6784, 2752, 2624, 3008,
  180. 2880, 2240, 2112, 2496, 2368, 3776, 3648,
  181. 4032, 3904, 3264, 3136, 3520, 3392, 22016,
  182. 20992, 24064, 23040, 17920, 16896, 19968, 18944,
  183. 30208, 29184, 32256, 31232, 26112, 25088, 28160,
  184. 27136, 11008, 10496, 12032, 11520, 8960, 8448,
  185. 9984, 9472, 15104, 14592, 16128, 15616, 13056,
  186. 12544, 14080, 13568, 344, 328, 376, 360,
  187. 280, 264, 312, 296, 472, 456, 504,
  188. 488, 408, 392, 440, 424, 88, 72,
  189. 120, 104, 24, 8, 56, 40, 216,
  190. 200, 248, 232, 152, 136, 184, 168,
  191. 1376, 1312, 1504, 1440, 1120, 1056, 1248,
  192. 1184, 1888, 1824, 2016, 1952, 1632, 1568,
  193. 1760, 1696, 688, 656, 752, 720, 560,
  194. 528, 624, 592, 944, 912, 1008, 976,
  195. 816, 784, 880, 848
  196. };
  197. /*
  198. * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
  199. * stored in a unsigned char. This function should only be called with
  200. * the data shifted such that it only contains information in the lower
  201. * 13-bits.
  202. *
  203. * Linear Input Code Compressed Code
  204. * ------------------------ ---------------
  205. * 0000000wxyza 000wxyz
  206. * 0000001wxyza 001wxyz
  207. * 000001wxyzab 010wxyz
  208. * 00001wxyzabc 011wxyz
  209. * 0001wxyzabcd 100wxyz
  210. * 001wxyzabcde 101wxyz
  211. * 01wxyzabcdef 110wxyz
  212. * 1wxyzabcdefg 111wxyz
  213. *
  214. * For further information see John C. Bellamy's Digital Telephony, 1982,
  215. * John Wiley & Sons, pps 98-111 and 472-476.
  216. */
  217. static unsigned char
  218. st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
  219. {
  220. PyInt16 mask;
  221. short seg;
  222. unsigned char aval;
  223. /* The original sox code does this in the calling function, not here */
  224. pcm_val = pcm_val >> 3;
  225. /* A-law using even bit inversion */
  226. if (pcm_val >= 0) {
  227. mask = 0xD5; /* sign (7th) bit = 1 */
  228. } else {
  229. mask = 0x55; /* sign bit = 0 */
  230. pcm_val = -pcm_val - 1;
  231. }
  232. /* Convert the scaled magnitude to segment number. */
  233. seg = search(pcm_val, seg_aend, 8);
  234. /* Combine the sign, segment, and quantization bits. */
  235. if (seg >= 8) /* out of range, return maximum value. */
  236. return (unsigned char) (0x7F ^ mask);
  237. else {
  238. aval = (unsigned char) seg << SEG_SHIFT;
  239. if (seg < 2)
  240. aval |= (pcm_val >> 1) & QUANT_MASK;
  241. else
  242. aval |= (pcm_val >> seg) & QUANT_MASK;
  243. return (aval ^ mask);
  244. }
  245. }
  246. /* End of code taken from sox */
  247. /* Intel ADPCM step variation table */
  248. static int indexTable[16] = {
  249. -1, -1, -1, -1, 2, 4, 6, 8,
  250. -1, -1, -1, -1, 2, 4, 6, 8,
  251. };
  252. static int stepsizeTable[89] = {
  253. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  254. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  255. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  256. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  257. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  258. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  259. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  260. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  261. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  262. };
  263. #define CHARP(cp, i) ((signed char *)(cp+i))
  264. #define SHORTP(cp, i) ((short *)(cp+i))
  265. #define LONGP(cp, i) ((Py_Int32 *)(cp+i))
  266. static PyObject *AudioopError;
  267. static PyObject *
  268. audioop_getsample(PyObject *self, PyObject *args)
  269. {
  270. signed char *cp;
  271. int len, size, val = 0;
  272. int i;
  273. if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
  274. return 0;
  275. if ( size != 1 && size != 2 && size != 4 ) {
  276. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  277. return 0;
  278. }
  279. if ( i < 0 || i >= len/size ) {
  280. PyErr_SetString(AudioopError, "Index out of range");
  281. return 0;
  282. }
  283. if ( size == 1 ) val = (int)*CHARP(cp, i);
  284. else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
  285. else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
  286. return PyInt_FromLong(val);
  287. }
  288. static PyObject *
  289. audioop_max(PyObject *self, PyObject *args)
  290. {
  291. signed char *cp;
  292. int len, size, val = 0;
  293. int i;
  294. int max = 0;
  295. if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
  296. return 0;
  297. if ( size != 1 && size != 2 && size != 4 ) {
  298. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  299. return 0;
  300. }
  301. for ( i=0; i<len; i+= size) {
  302. if ( size == 1 ) val = (int)*CHARP(cp, i);
  303. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  304. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  305. if ( val < 0 ) val = (-val);
  306. if ( val > max ) max = val;
  307. }
  308. return PyInt_FromLong(max);
  309. }
  310. static PyObject *
  311. audioop_minmax(PyObject *self, PyObject *args)
  312. {
  313. signed char *cp;
  314. int len, size, val = 0;
  315. int i;
  316. int min = 0x7fffffff, max = -0x7fffffff;
  317. if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
  318. return NULL;
  319. if (size != 1 && size != 2 && size != 4) {
  320. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  321. return NULL;
  322. }
  323. for (i = 0; i < len; i += size) {
  324. if (size == 1) val = (int) *CHARP(cp, i);
  325. else if (size == 2) val = (int) *SHORTP(cp, i);
  326. else if (size == 4) val = (int) *LONGP(cp, i);
  327. if (val > max) max = val;
  328. if (val < min) min = val;
  329. }
  330. return Py_BuildValue("(ii)", min, max);
  331. }
  332. static PyObject *
  333. audioop_avg(PyObject *self, PyObject *args)
  334. {
  335. signed char *cp;
  336. int len, size, val = 0;
  337. int i;
  338. double avg = 0.0;
  339. if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
  340. return 0;
  341. if ( size != 1 && size != 2 && size != 4 ) {
  342. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  343. return 0;
  344. }
  345. for ( i=0; i<len; i+= size) {
  346. if ( size == 1 ) val = (int)*CHARP(cp, i);
  347. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  348. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  349. avg += val;
  350. }
  351. if ( len == 0 )
  352. val = 0;
  353. else
  354. val = (int)(avg / (double)(len/size));
  355. return PyInt_FromLong(val);
  356. }
  357. static PyObject *
  358. audioop_rms(PyObject *self, PyObject *args)
  359. {
  360. signed char *cp;
  361. int len, size, val = 0;
  362. int i;
  363. double sum_squares = 0.0;
  364. if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
  365. return 0;
  366. if ( size != 1 && size != 2 && size != 4 ) {
  367. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  368. return 0;
  369. }
  370. for ( i=0; i<len; i+= size) {
  371. if ( size == 1 ) val = (int)*CHARP(cp, i);
  372. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  373. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  374. sum_squares += (double)val*(double)val;
  375. }
  376. if ( len == 0 )
  377. val = 0;
  378. else
  379. val = (int)sqrt(sum_squares / (double)(len/size));
  380. return PyInt_FromLong(val);
  381. }
  382. static double _sum2(short *a, short *b, int len)
  383. {
  384. int i;
  385. double sum = 0.0;
  386. for( i=0; i<len; i++) {
  387. sum = sum + (double)a[i]*(double)b[i];
  388. }
  389. return sum;
  390. }
  391. /*
  392. ** Findfit tries to locate a sample within another sample. Its main use
  393. ** is in echo-cancellation (to find the feedback of the output signal in
  394. ** the input signal).
  395. ** The method used is as follows:
  396. **
  397. ** let R be the reference signal (length n) and A the input signal (length N)
  398. ** with N > n, and let all sums be over i from 0 to n-1.
  399. **
  400. ** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
  401. ** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
  402. ** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
  403. **
  404. ** Next, we compute the relative distance between the original signal and
  405. ** the modified signal and minimize that over j:
  406. ** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 ) =>
  407. ** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
  408. **
  409. ** In the code variables correspond as follows:
  410. ** cp1 A
  411. ** cp2 R
  412. ** len1 N
  413. ** len2 n
  414. ** aj_m1 A[j-1]
  415. ** aj_lm1 A[j+n-1]
  416. ** sum_ri_2 sum(R[i]^2)
  417. ** sum_aij_2 sum(A[i+j]^2)
  418. ** sum_aij_ri sum(A[i+j]R[i])
  419. **
  420. ** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
  421. ** is completely recalculated each step.
  422. */
  423. static PyObject *
  424. audioop_findfit(PyObject *self, PyObject *args)
  425. {
  426. short *cp1, *cp2;
  427. int len1, len2;
  428. int j, best_j;
  429. double aj_m1, aj_lm1;
  430. double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
  431. /* Passing a short** for an 's' argument is correct only
  432. if the string contents is aligned for interpretation
  433. as short[]. Due to the definition of PyStringObject,
  434. this is currently (Python 2.6) the case. */
  435. if ( !PyArg_ParseTuple(args, "s#s#:findfit",
  436. (char**)&cp1, &len1, (char**)&cp2, &len2) )
  437. return 0;
  438. if ( len1 & 1 || len2 & 1 ) {
  439. PyErr_SetString(AudioopError, "Strings should be even-sized");
  440. return 0;
  441. }
  442. len1 >>= 1;
  443. len2 >>= 1;
  444. if ( len1 < len2 ) {
  445. PyErr_SetString(AudioopError, "First sample should be longer");
  446. return 0;
  447. }
  448. sum_ri_2 = _sum2(cp2, cp2, len2);
  449. sum_aij_2 = _sum2(cp1, cp1, len2);
  450. sum_aij_ri = _sum2(cp1, cp2, len2);
  451. result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
  452. best_result = result;
  453. best_j = 0;
  454. j = 0;
  455. for ( j=1; j<=len1-len2; j++) {
  456. aj_m1 = (double)cp1[j-1];
  457. aj_lm1 = (double)cp1[j+len2-1];
  458. sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
  459. sum_aij_ri = _sum2(cp1+j, cp2, len2);
  460. result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
  461. / sum_aij_2;
  462. if ( result < best_result ) {
  463. best_result = result;
  464. best_j = j;
  465. }
  466. }
  467. factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
  468. return Py_BuildValue("(if)", best_j, factor);
  469. }
  470. /*
  471. ** findfactor finds a factor f so that the energy in A-fB is minimal.
  472. ** See the comment for findfit for details.
  473. */
  474. static PyObject *
  475. audioop_findfactor(PyObject *self, PyObject *args)
  476. {
  477. short *cp1, *cp2;
  478. int len1, len2;
  479. double sum_ri_2, sum_aij_ri, result;
  480. if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
  481. (char**)&cp1, &len1, (char**)&cp2, &len2) )
  482. return 0;
  483. if ( len1 & 1 || len2 & 1 ) {
  484. PyErr_SetString(AudioopError, "Strings should be even-sized");
  485. return 0;
  486. }
  487. if ( len1 != len2 ) {
  488. PyErr_SetString(AudioopError, "Samples should be same size");
  489. return 0;
  490. }
  491. len2 >>= 1;
  492. sum_ri_2 = _sum2(cp2, cp2, len2);
  493. sum_aij_ri = _sum2(cp1, cp2, len2);
  494. result = sum_aij_ri / sum_ri_2;
  495. return PyFloat_FromDouble(result);
  496. }
  497. /*
  498. ** findmax returns the index of the n-sized segment of the input sample
  499. ** that contains the most energy.
  500. */
  501. static PyObject *
  502. audioop_findmax(PyObject *self, PyObject *args)
  503. {
  504. short *cp1;
  505. int len1, len2;
  506. int j, best_j;
  507. double aj_m1, aj_lm1;
  508. double result, best_result;
  509. if ( !PyArg_ParseTuple(args, "s#i:findmax",
  510. (char**)&cp1, &len1, &len2) )
  511. return 0;
  512. if ( len1 & 1 ) {
  513. PyErr_SetString(AudioopError, "Strings should be even-sized");
  514. return 0;
  515. }
  516. len1 >>= 1;
  517. if ( len2 < 0 || len1 < len2 ) {
  518. PyErr_SetString(AudioopError, "Input sample should be longer");
  519. return 0;
  520. }
  521. result = _sum2(cp1, cp1, len2);
  522. best_result = result;
  523. best_j = 0;
  524. j = 0;
  525. for ( j=1; j<=len1-len2; j++) {
  526. aj_m1 = (double)cp1[j-1];
  527. aj_lm1 = (double)cp1[j+len2-1];
  528. result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
  529. if ( result > best_result ) {
  530. best_result = result;
  531. best_j = j;
  532. }
  533. }
  534. return PyInt_FromLong(best_j);
  535. }
  536. static PyObject *
  537. audioop_avgpp(PyObject *self, PyObject *args)
  538. {
  539. signed char *cp;
  540. int len, size, val = 0, prevval = 0, prevextremevalid = 0,
  541. prevextreme = 0;
  542. int i;
  543. double avg = 0.0;
  544. int diff, prevdiff, extremediff, nextreme = 0;
  545. if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
  546. return 0;
  547. if ( size != 1 && size != 2 && size != 4 ) {
  548. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  549. return 0;
  550. }
  551. /* Compute first delta value ahead. Also automatically makes us
  552. ** skip the first extreme value
  553. */
  554. if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
  555. else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
  556. else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
  557. if ( size == 1 ) val = (int)*CHARP(cp, size);
  558. else if ( size == 2 ) val = (int)*SHORTP(cp, size);
  559. else if ( size == 4 ) val = (int)*LONGP(cp, size);
  560. prevdiff = val - prevval;
  561. for ( i=size; i<len; i+= size) {
  562. if ( size == 1 ) val = (int)*CHARP(cp, i);
  563. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  564. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  565. diff = val - prevval;
  566. if ( diff*prevdiff < 0 ) {
  567. /* Derivative changed sign. Compute difference to last
  568. ** extreme value and remember.
  569. */
  570. if ( prevextremevalid ) {
  571. extremediff = prevval - prevextreme;
  572. if ( extremediff < 0 )
  573. extremediff = -extremediff;
  574. avg += extremediff;
  575. nextreme++;
  576. }
  577. prevextremevalid = 1;
  578. prevextreme = prevval;
  579. }
  580. prevval = val;
  581. if ( diff != 0 )
  582. prevdiff = diff;
  583. }
  584. if ( nextreme == 0 )
  585. val = 0;
  586. else
  587. val = (int)(avg / (double)nextreme);
  588. return PyInt_FromLong(val);
  589. }
  590. static PyObject *
  591. audioop_maxpp(PyObject *self, PyObject *args)
  592. {
  593. signed char *cp;
  594. int len, size, val = 0, prevval = 0, prevextremevalid = 0,
  595. prevextreme = 0;
  596. int i;
  597. int max = 0;
  598. int diff, prevdiff, extremediff;
  599. if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
  600. return 0;
  601. if ( size != 1 && size != 2 && size != 4 ) {
  602. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  603. return 0;
  604. }
  605. /* Compute first delta value ahead. Also automatically makes us
  606. ** skip the first extreme value
  607. */
  608. if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
  609. else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
  610. else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
  611. if ( size == 1 ) val = (int)*CHARP(cp, size);
  612. else if ( size == 2 ) val = (int)*SHORTP(cp, size);
  613. else if ( size == 4 ) val = (int)*LONGP(cp, size);
  614. prevdiff = val - prevval;
  615. for ( i=size; i<len; i+= size) {
  616. if ( size == 1 ) val = (int)*CHARP(cp, i);
  617. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  618. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  619. diff = val - prevval;
  620. if ( diff*prevdiff < 0 ) {
  621. /* Derivative changed sign. Compute difference to
  622. ** last extreme value and remember.
  623. */
  624. if ( prevextremevalid ) {
  625. extremediff = prevval - prevextreme;
  626. if ( extremediff < 0 )
  627. extremediff = -extremediff;
  628. if ( extremediff > max )
  629. max = extremediff;
  630. }
  631. prevextremevalid = 1;
  632. prevextreme = prevval;
  633. }
  634. prevval = val;
  635. if ( diff != 0 )
  636. prevdiff = diff;
  637. }
  638. return PyInt_FromLong(max);
  639. }
  640. static PyObject *
  641. audioop_cross(PyObject *self, PyObject *args)
  642. {
  643. signed char *cp;
  644. int len, size, val = 0;
  645. int i;
  646. int prevval, ncross;
  647. if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
  648. return 0;
  649. if ( size != 1 && size != 2 && size != 4 ) {
  650. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  651. return 0;
  652. }
  653. ncross = -1;
  654. prevval = 17; /* Anything <> 0,1 */
  655. for ( i=0; i<len; i+= size) {
  656. if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
  657. else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
  658. else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
  659. val = val & 1;
  660. if ( val != prevval ) ncross++;
  661. prevval = val;
  662. }
  663. return PyInt_FromLong(ncross);
  664. }
  665. static PyObject *
  666. audioop_mul(PyObject *self, PyObject *args)
  667. {
  668. signed char *cp, *ncp;
  669. int len, size, val = 0;
  670. double factor, fval, maxval;
  671. PyObject *rv;
  672. int i;
  673. if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
  674. return 0;
  675. if ( size == 1 ) maxval = (double) 0x7f;
  676. else if ( size == 2 ) maxval = (double) 0x7fff;
  677. else if ( size == 4 ) maxval = (double) 0x7fffffff;
  678. else {
  679. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  680. return 0;
  681. }
  682. rv = PyString_FromStringAndSize(NULL, len);
  683. if ( rv == 0 )
  684. return 0;
  685. ncp = (signed char *)PyString_AsString(rv);
  686. for ( i=0; i < len; i += size ) {
  687. if ( size == 1 ) val = (int)*CHARP(cp, i);
  688. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  689. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  690. fval = (double)val*factor;
  691. if ( fval > maxval ) fval = maxval;
  692. else if ( fval < -maxval ) fval = -maxval;
  693. val = (int)fval;
  694. if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
  695. else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
  696. else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
  697. }
  698. return rv;
  699. }
  700. static PyObject *
  701. audioop_tomono(PyObject *self, PyObject *args)
  702. {
  703. signed char *cp, *ncp;
  704. int len, size, val1 = 0, val2 = 0;
  705. double fac1, fac2, fval, maxval;
  706. PyObject *rv;
  707. int i;
  708. if ( !PyArg_ParseTuple(args, "s#idd:tomono",
  709. &cp, &len, &size, &fac1, &fac2 ) )
  710. return 0;
  711. if ( size == 1 ) maxval = (double) 0x7f;
  712. else if ( size == 2 ) maxval = (double) 0x7fff;
  713. else if ( size == 4 ) maxval = (double) 0x7fffffff;
  714. else {
  715. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  716. return 0;
  717. }
  718. rv = PyString_FromStringAndSize(NULL, len/2);
  719. if ( rv == 0 )
  720. return 0;
  721. ncp = (signed char *)PyString_AsString(rv);
  722. for ( i=0; i < len; i += size*2 ) {
  723. if ( size == 1 ) val1 = (int)*CHARP(cp, i);
  724. else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
  725. else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
  726. if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
  727. else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
  728. else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
  729. fval = (double)val1*fac1 + (double)val2*fac2;
  730. if ( fval > maxval ) fval = maxval;
  731. else if ( fval < -maxval ) fval = -maxval;
  732. val1 = (int)fval;
  733. if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
  734. else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
  735. else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
  736. }
  737. return rv;
  738. }
  739. static PyObject *
  740. audioop_tostereo(PyObject *self, PyObject *args)
  741. {
  742. signed char *cp, *ncp;
  743. int len, new_len, size, val1, val2, val = 0;
  744. double fac1, fac2, fval, maxval;
  745. PyObject *rv;
  746. int i;
  747. if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
  748. &cp, &len, &size, &fac1, &fac2 ) )
  749. return 0;
  750. if ( size == 1 ) maxval = (double) 0x7f;
  751. else if ( size == 2 ) maxval = (double) 0x7fff;
  752. else if ( size == 4 ) maxval = (double) 0x7fffffff;
  753. else {
  754. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  755. return 0;
  756. }
  757. new_len = len*2;
  758. if (new_len < 0) {
  759. PyErr_SetString(PyExc_MemoryError,
  760. "not enough memory for output buffer");
  761. return 0;
  762. }
  763. rv = PyString_FromStringAndSize(NULL, new_len);
  764. if ( rv == 0 )
  765. return 0;
  766. ncp = (signed char *)PyString_AsString(rv);
  767. for ( i=0; i < len; i += size ) {
  768. if ( size == 1 ) val = (int)*CHARP(cp, i);
  769. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  770. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  771. fval = (double)val*fac1;
  772. if ( fval > maxval ) fval = maxval;
  773. else if ( fval < -maxval ) fval = -maxval;
  774. val1 = (int)fval;
  775. fval = (double)val*fac2;
  776. if ( fval > maxval ) fval = maxval;
  777. else if ( fval < -maxval ) fval = -maxval;
  778. val2 = (int)fval;
  779. if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
  780. else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
  781. else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
  782. if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
  783. else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
  784. else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
  785. }
  786. return rv;
  787. }
  788. static PyObject *
  789. audioop_add(PyObject *self, PyObject *args)
  790. {
  791. signed char *cp1, *cp2, *ncp;
  792. int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
  793. PyObject *rv;
  794. int i;
  795. if ( !PyArg_ParseTuple(args, "s#s#i:add",
  796. &cp1, &len1, &cp2, &len2, &size ) )
  797. return 0;
  798. if ( len1 != len2 ) {
  799. PyErr_SetString(AudioopError, "Lengths should be the same");
  800. return 0;
  801. }
  802. if ( size == 1 ) maxval = 0x7f;
  803. else if ( size == 2 ) maxval = 0x7fff;
  804. else if ( size == 4 ) maxval = 0x7fffffff;
  805. else {
  806. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  807. return 0;
  808. }
  809. rv = PyString_FromStringAndSize(NULL, len1);
  810. if ( rv == 0 )
  811. return 0;
  812. ncp = (signed char *)PyString_AsString(rv);
  813. for ( i=0; i < len1; i += size ) {
  814. if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
  815. else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
  816. else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
  817. if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
  818. else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
  819. else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
  820. newval = val1 + val2;
  821. /* truncate in case of overflow */
  822. if (newval > maxval) newval = maxval;
  823. else if (newval < -maxval) newval = -maxval;
  824. else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
  825. newval = val1 > 0 ? maxval : - maxval;
  826. if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
  827. else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
  828. else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
  829. }
  830. return rv;
  831. }
  832. static PyObject *
  833. audioop_bias(PyObject *self, PyObject *args)
  834. {
  835. signed char *cp, *ncp;
  836. int len, size, val = 0;
  837. PyObject *rv;
  838. int i;
  839. int bias;
  840. if ( !PyArg_ParseTuple(args, "s#ii:bias",
  841. &cp, &len, &size , &bias) )
  842. return 0;
  843. if ( size != 1 && size != 2 && size != 4) {
  844. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  845. return 0;
  846. }
  847. rv = PyString_FromStringAndSize(NULL, len);
  848. if ( rv == 0 )
  849. return 0;
  850. ncp = (signed char *)PyString_AsString(rv);
  851. for ( i=0; i < len; i += size ) {
  852. if ( size == 1 ) val = (int)*CHARP(cp, i);
  853. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  854. else if ( size == 4 ) val = (int)*LONGP(cp, i);
  855. if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
  856. else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
  857. else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
  858. }
  859. return rv;
  860. }
  861. static PyObject *
  862. audioop_reverse(PyObject *self, PyObject *args)
  863. {
  864. signed char *cp;
  865. unsigned char *ncp;
  866. int len, size, val = 0;
  867. PyObject *rv;
  868. int i, j;
  869. if ( !PyArg_ParseTuple(args, "s#i:reverse",
  870. &cp, &len, &size) )
  871. return 0;
  872. if ( size != 1 && size != 2 && size != 4 ) {
  873. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  874. return 0;
  875. }
  876. rv = PyString_FromStringAndSize(NULL, len);
  877. if ( rv == 0 )
  878. return 0;
  879. ncp = (unsigned char *)PyString_AsString(rv);
  880. for ( i=0; i < len; i += size ) {
  881. if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
  882. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  883. else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  884. j = len - i - size;
  885. if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
  886. else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
  887. else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
  888. }
  889. return rv;
  890. }
  891. static PyObject *
  892. audioop_lin2lin(PyObject *self, PyObject *args)
  893. {
  894. signed char *cp;
  895. unsigned char *ncp;
  896. int len, new_len, size, size2, val = 0;
  897. PyObject *rv;
  898. int i, j;
  899. if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
  900. &cp, &len, &size, &size2) )
  901. return 0;
  902. if ( (size != 1 && size != 2 && size != 4) ||
  903. (size2 != 1 && size2 != 2 && size2 != 4)) {
  904. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  905. return 0;
  906. }
  907. new_len = (len/size)*size2;
  908. if (new_len < 0) {
  909. PyErr_SetString(PyExc_MemoryError,
  910. "not enough memory for output buffer");
  911. return 0;
  912. }
  913. rv = PyString_FromStringAndSize(NULL, new_len);
  914. if ( rv == 0 )
  915. return 0;
  916. ncp = (unsigned char *)PyString_AsString(rv);
  917. for ( i=0, j=0; i < len; i += size, j += size2 ) {
  918. if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
  919. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  920. else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  921. if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
  922. else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
  923. else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
  924. }
  925. return rv;
  926. }
  927. static int
  928. gcd(int a, int b)
  929. {
  930. while (b > 0) {
  931. int tmp = a % b;
  932. a = b;
  933. b = tmp;
  934. }
  935. return a;
  936. }
  937. static PyObject *
  938. audioop_ratecv(PyObject *self, PyObject *args)
  939. {
  940. char *cp, *ncp;
  941. int len, size, nchannels, inrate, outrate, weightA, weightB;
  942. int chan, d, *prev_i, *cur_i, cur_o;
  943. PyObject *state, *samps, *str, *rv = NULL;
  944. int bytes_per_frame;
  945. size_t alloc_size;
  946. weightA = 1;
  947. weightB = 0;
  948. if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
  949. &nchannels, &inrate, &outrate, &state,
  950. &weightA, &weightB))
  951. return NULL;
  952. if (size != 1 && size != 2 && size != 4) {
  953. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  954. return NULL;
  955. }
  956. if (nchannels < 1) {
  957. PyErr_SetString(AudioopError, "# of channels should be >= 1");
  958. return NULL;
  959. }
  960. bytes_per_frame = size * nchannels;
  961. if (bytes_per_frame / nchannels != size) {
  962. /* This overflow test is rigorously correct because
  963. both multiplicands are >= 1. Use the argument names
  964. from the docs for the error msg. */
  965. PyErr_SetString(PyExc_OverflowError,
  966. "width * nchannels too big for a C int");
  967. return NULL;
  968. }
  969. if (weightA < 1 || weightB < 0) {
  970. PyErr_SetString(AudioopError,
  971. "weightA should be >= 1, weightB should be >= 0");
  972. return NULL;
  973. }
  974. if (len % bytes_per_frame != 0) {
  975. PyErr_SetString(AudioopError, "not a whole number of frames");
  976. return NULL;
  977. }
  978. if (inrate <= 0 || outrate <= 0) {
  979. PyErr_SetString(AudioopError, "sampling rate not > 0");
  980. return NULL;
  981. }
  982. /* divide inrate and outrate by their greatest common divisor */
  983. d = gcd(inrate, outrate);
  984. inrate /= d;
  985. outrate /= d;
  986. alloc_size = sizeof(int) * (unsigned)nchannels;
  987. if (alloc_size < nchannels) {
  988. PyErr_SetString(PyExc_MemoryError,
  989. "not enough memory for output buffer");
  990. return 0;
  991. }
  992. prev_i = (int *) malloc(alloc_size);
  993. cur_i = (int *) malloc(alloc_size);
  994. if (prev_i == NULL || cur_i == NULL) {
  995. (void) PyErr_NoMemory();
  996. goto exit;
  997. }
  998. len /= bytes_per_frame; /* # of frames */
  999. if (state == Py_None) {
  1000. d = -outrate;
  1001. for (chan = 0; chan < nchannels; chan++)
  1002. prev_i[chan] = cur_i[chan] = 0;
  1003. }
  1004. else {
  1005. if (!PyArg_ParseTuple(state,
  1006. "iO!;audioop.ratecv: illegal state argument",
  1007. &d, &PyTuple_Type, &samps))
  1008. goto exit;
  1009. if (PyTuple_Size(samps) != nchannels) {
  1010. PyErr_SetString(AudioopError,
  1011. "illegal state argument");
  1012. goto exit;
  1013. }
  1014. for (chan = 0; chan < nchannels; chan++) {
  1015. if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
  1016. "ii:ratecv", &prev_i[chan],
  1017. &cur_i[chan]))
  1018. goto exit;
  1019. }
  1020. }
  1021. /* str <- Space for the output buffer. */
  1022. {
  1023. /* There are len input frames, so we need (mathematically)
  1024. ceiling(len*outrate/inrate) output frames, and each frame
  1025. requires bytes_per_frame bytes. Computing this
  1026. without spurious overflow is the challenge; we can
  1027. settle for a reasonable upper bound, though. */
  1028. int ceiling; /* the number of output frames */
  1029. int nbytes; /* the number of output bytes needed */
  1030. int q = len / inrate;
  1031. /* Now len = q * inrate + r exactly (with r = len % inrate),
  1032. and this is less than q * inrate + inrate = (q+1)*inrate.
  1033. So a reasonable upper bound on len*outrate/inrate is
  1034. ((q+1)*inrate)*outrate/inrate =
  1035. (q+1)*outrate.
  1036. */
  1037. ceiling = (q+1) * outrate;
  1038. nbytes = ceiling * bytes_per_frame;
  1039. /* See whether anything overflowed; if not, get the space. */
  1040. if (q+1 < 0 ||
  1041. ceiling / outrate != q+1 ||
  1042. nbytes / bytes_per_frame != ceiling)
  1043. str = NULL;
  1044. else
  1045. str = PyString_FromStringAndSize(NULL, nbytes);
  1046. if (str == NULL) {
  1047. PyErr_SetString(PyExc_MemoryError,
  1048. "not enough memory for output buffer");
  1049. goto exit;
  1050. }
  1051. }
  1052. ncp = PyString_AsString(str);
  1053. for (;;) {
  1054. while (d < 0) {
  1055. if (len == 0) {
  1056. samps = PyTuple_New(nchannels);
  1057. if (samps == NULL)
  1058. goto exit;
  1059. for (chan = 0; chan < nchannels; chan++)
  1060. PyTuple_SetItem(samps, chan,
  1061. Py_BuildValue("(ii)",
  1062. prev_i[chan],
  1063. cur_i[chan]));
  1064. if (PyErr_Occurred())
  1065. goto exit;
  1066. /* We have checked before that the length
  1067. * of the string fits into int. */
  1068. len = (int)(ncp - PyString_AsString(str));
  1069. if (len == 0) {
  1070. /*don't want to resize to zero length*/
  1071. rv = PyString_FromStringAndSize("", 0);
  1072. Py_DECREF(str);
  1073. str = rv;
  1074. } else if (_PyString_Resize(&str, len) < 0)
  1075. goto exit;
  1076. rv = Py_BuildValue("(O(iO))", str, d, samps);
  1077. Py_DECREF(samps);
  1078. Py_DECREF(str);
  1079. goto exit; /* return rv */
  1080. }
  1081. for (chan = 0; chan < nchannels; chan++) {
  1082. prev_i[chan] = cur_i[chan];
  1083. if (size == 1)
  1084. cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
  1085. else if (size == 2)
  1086. cur_i[chan] = (int)*SHORTP(cp, 0);
  1087. else if (size == 4)
  1088. cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
  1089. cp += size;
  1090. /* implements a simple digital filter */
  1091. cur_i[chan] =
  1092. (weightA * cur_i[chan] +
  1093. weightB * prev_i[chan]) /
  1094. (weightA + weightB);
  1095. }
  1096. len--;
  1097. d += outrate;
  1098. }
  1099. while (d >= 0) {
  1100. for (chan = 0; chan < nchannels; chan++) {
  1101. cur_o = (prev_i[chan] * d +
  1102. cur_i[chan] * (outrate - d)) /
  1103. outrate;
  1104. if (size == 1)
  1105. *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
  1106. else if (size == 2)
  1107. *SHORTP(ncp, 0) = (short)(cur_o);
  1108. else if (size == 4)
  1109. *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
  1110. ncp += size;
  1111. }
  1112. d -= inrate;
  1113. }
  1114. }
  1115. exit:
  1116. if (prev_i != NULL)
  1117. free(prev_i);
  1118. if (cur_i != NULL)
  1119. free(cur_i);
  1120. return rv;
  1121. }
  1122. static PyObject *
  1123. audioop_lin2ulaw(PyObject *self, PyObject *args)
  1124. {
  1125. signed char *cp;
  1126. unsigned char *ncp;
  1127. int len, size, val = 0;
  1128. PyObject *rv;
  1129. int i;
  1130. if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
  1131. &cp, &len, &size) )
  1132. return 0 ;
  1133. if ( size != 1 && size != 2 && size != 4) {
  1134. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1135. return 0;
  1136. }
  1137. rv = PyString_FromStringAndSize(NULL, len/size);
  1138. if ( rv == 0 )
  1139. return 0;
  1140. ncp = (unsigned char *)PyString_AsString(rv);
  1141. for ( i=0; i < len; i += size ) {
  1142. if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
  1143. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  1144. else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  1145. *ncp++ = st_14linear2ulaw(val);
  1146. }
  1147. return rv;
  1148. }
  1149. static PyObject *
  1150. audioop_ulaw2lin(PyObject *self, PyObject *args)
  1151. {
  1152. unsigned char *cp;
  1153. unsigned char cval;
  1154. signed char *ncp;
  1155. int len, new_len, size, val;
  1156. PyObject *rv;
  1157. int i;
  1158. if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
  1159. &cp, &len, &size) )
  1160. return 0;
  1161. if ( size != 1 && size != 2 && size != 4) {
  1162. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1163. return 0;
  1164. }
  1165. new_len = len*size;
  1166. if (new_len < 0) {
  1167. PyErr_SetString(PyExc_MemoryError,
  1168. "not enough memory for output buffer");
  1169. return 0;
  1170. }
  1171. rv = PyString_FromStringAndSize(NULL, new_len);
  1172. if ( rv == 0 )
  1173. return 0;
  1174. ncp = (signed char *)PyString_AsString(rv);
  1175. for ( i=0; i < new_len; i += size ) {
  1176. cval = *cp++;
  1177. val = st_ulaw2linear16(cval);
  1178. if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
  1179. else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
  1180. else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
  1181. }
  1182. return rv;
  1183. }
  1184. static PyObject *
  1185. audioop_lin2alaw(PyObject *self, PyObject *args)
  1186. {
  1187. signed char *cp;
  1188. unsigned char *ncp;
  1189. int len, size, val = 0;
  1190. PyObject *rv;
  1191. int i;
  1192. if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
  1193. &cp, &len, &size) )
  1194. return 0;
  1195. if ( size != 1 && size != 2 && size != 4) {
  1196. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1197. return 0;
  1198. }
  1199. rv = PyString_FromStringAndSize(NULL, len/size);
  1200. if ( rv == 0 )
  1201. return 0;
  1202. ncp = (unsigned char *)PyString_AsString(rv);
  1203. for ( i=0; i < len; i += size ) {
  1204. if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
  1205. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  1206. else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  1207. *ncp++ = st_linear2alaw(val);
  1208. }
  1209. return rv;
  1210. }
  1211. static PyObject *
  1212. audioop_alaw2lin(PyObject *self, PyObject *args)
  1213. {
  1214. unsigned char *cp;
  1215. unsigned char cval;
  1216. signed char *ncp;
  1217. int len, new_len, size, val;
  1218. PyObject *rv;
  1219. int i;
  1220. if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
  1221. &cp, &len, &size) )
  1222. return 0;
  1223. if ( size != 1 && size != 2 && size != 4) {
  1224. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1225. return 0;
  1226. }
  1227. new_len = len*size;
  1228. if (new_len < 0) {
  1229. PyErr_SetString(PyExc_MemoryError,
  1230. "not enough memory for output buffer");
  1231. return 0;
  1232. }
  1233. rv = PyString_FromStringAndSize(NULL, new_len);
  1234. if ( rv == 0 )
  1235. return 0;
  1236. ncp = (signed char *)PyString_AsString(rv);
  1237. for ( i=0; i < new_len; i += size ) {
  1238. cval = *cp++;
  1239. val = st_alaw2linear16(cval);
  1240. if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
  1241. else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
  1242. else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
  1243. }
  1244. return rv;
  1245. }
  1246. static PyObject *
  1247. audioop_lin2adpcm(PyObject *self, PyObject *args)
  1248. {
  1249. signed char *cp;
  1250. signed char *ncp;
  1251. int len, size, val = 0, step, valpred, delta,
  1252. index, sign, vpdiff, diff;
  1253. PyObject *rv, *state, *str;
  1254. int i, outputbuffer = 0, bufferstep;
  1255. if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
  1256. &cp, &len, &size, &state) )
  1257. return 0;
  1258. if ( size != 1 && size != 2 && size != 4) {
  1259. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1260. return 0;
  1261. }
  1262. str = PyString_FromStringAndSize(NULL, len/(size*2));
  1263. if ( str == 0 )
  1264. return 0;
  1265. ncp = (signed char *)PyString_AsString(str);
  1266. /* Decode state, should have (value, step) */
  1267. if ( state == Py_None ) {
  1268. /* First time, it seems. Set defaults */
  1269. valpred = 0;
  1270. step = 7;
  1271. index = 0;
  1272. } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
  1273. return 0;
  1274. step = stepsizeTable[index];
  1275. bufferstep = 1;
  1276. for ( i=0; i < len; i += size ) {
  1277. if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
  1278. else if ( size == 2 ) val = (int)*SHORTP(cp, i);
  1279. else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
  1280. /* Step 1 - compute difference with previous value */
  1281. diff = val - valpred;
  1282. sign = (diff < 0) ? 8 : 0;
  1283. if ( sign ) diff = (-diff);
  1284. /* Step 2 - Divide and clamp */
  1285. /* Note:
  1286. ** This code *approximately* computes:
  1287. ** delta = diff*4/step;
  1288. ** vpdiff = (delta+0.5)*step/4;
  1289. ** but in shift step bits are dropped. The net result of this
  1290. ** is that even if you have fast mul/div hardware you cannot
  1291. ** put it to good use since the fixup would be too expensive.
  1292. */
  1293. delta = 0;
  1294. vpdiff = (step >> 3);
  1295. if ( diff >= step ) {
  1296. delta = 4;
  1297. diff -= step;
  1298. vpdiff += step;
  1299. }
  1300. step >>= 1;
  1301. if ( diff >= step ) {
  1302. delta |= 2;
  1303. diff -= step;
  1304. vpdiff += step;
  1305. }
  1306. step >>= 1;
  1307. if ( diff >= step ) {
  1308. delta |= 1;
  1309. vpdiff += step;
  1310. }
  1311. /* Step 3 - Update previous value */
  1312. if ( sign )
  1313. valpred -= vpdiff;
  1314. else
  1315. valpred += vpdiff;
  1316. /* Step 4 - Clamp previous value to 16 bits */
  1317. if ( valpred > 32767 )
  1318. valpred = 32767;
  1319. else if ( valpred < -32768 )
  1320. valpred = -32768;
  1321. /* Step 5 - Assemble value, update index and step values */
  1322. delta |= sign;
  1323. index += indexTable[delta];
  1324. if ( index < 0 ) index = 0;
  1325. if ( index > 88 ) index = 88;
  1326. step = stepsizeTable[index];
  1327. /* Step 6 - Output value */
  1328. if ( bufferstep ) {
  1329. outputbuffer = (delta << 4) & 0xf0;
  1330. } else {
  1331. *ncp++ = (delta & 0x0f) | outputbuffer;
  1332. }
  1333. bufferstep = !bufferstep;
  1334. }
  1335. rv = Py_BuildValue("(O(ii))", str, valpred, index);
  1336. Py_DECREF(str);
  1337. return rv;
  1338. }
  1339. static PyObject *
  1340. audioop_adpcm2lin(PyObject *self, PyObject *args)
  1341. {
  1342. signed char *cp;
  1343. signed char *ncp;
  1344. int len, new_len, size, valpred, step, delta, index, sign, vpdiff;
  1345. PyObject *rv, *str, *state;
  1346. int i, inputbuffer = 0, bufferstep;
  1347. if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
  1348. &cp, &len, &size, &state) )
  1349. return 0;
  1350. if ( size != 1 && size != 2 && size != 4) {
  1351. PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
  1352. return 0;
  1353. }
  1354. /* Decode state, should have (value, step) */
  1355. if ( state == Py_None ) {
  1356. /* First time, it seems. Set defaults */
  1357. valpred = 0;
  1358. step = 7;
  1359. index = 0;
  1360. } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
  1361. return 0;
  1362. new_len = len*size*2;
  1363. if (new_len < 0) {
  1364. PyErr_SetString(PyExc_MemoryError,
  1365. "not enough memory for output buffer");
  1366. return 0;
  1367. }
  1368. str = PyString_FromStringAndSize(NULL, new_len);
  1369. if ( str == 0 )
  1370. return 0;
  1371. ncp = (signed char *)PyString_AsString(str);
  1372. step = stepsizeTable[index];
  1373. bufferstep = 0;
  1374. for ( i=0; i < new_len; i += size ) {
  1375. /* Step 1 - get the delta value and compute next index */
  1376. if ( bufferstep ) {
  1377. delta = inputbuffer & 0xf;
  1378. } else {
  1379. inputbuffer = *cp++;
  1380. delta = (inputbuffer >> 4) & 0xf;
  1381. }
  1382. bufferstep = !bufferstep;
  1383. /* Step 2 - Find new index value (for later) */
  1384. index += indexTable[delta];
  1385. if ( index < 0 ) index = 0;
  1386. if ( index > 88 ) index = 88;
  1387. /* Step 3 - Separate sign and magnitude */
  1388. sign = delta & 8;
  1389. delta = delta & 7;
  1390. /* Step 4 - Compute difference and new predicted value */
  1391. /*
  1392. ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
  1393. ** in adpcm_coder.
  1394. */
  1395. vpdiff = step >> 3;
  1396. if ( delta & 4 ) vpdiff += step;
  1397. if ( delta & 2 ) vpdiff += step>>1;
  1398. if ( delta & 1 ) vpdiff += step>>2;
  1399. if ( sign )
  1400. valpred -= vpdiff;
  1401. else
  1402. valpred += vpdiff;
  1403. /* Step 5 - clamp output value */
  1404. if ( valpred > 32767 )
  1405. valpred = 32767;
  1406. else if ( valpred < -32768 )
  1407. valpred = -32768;
  1408. /* Step 6 - Update step value */
  1409. step = stepsizeTable[index];
  1410. /* Step 6 - Output value */
  1411. if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
  1412. else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
  1413. else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
  1414. }
  1415. rv = Py_BuildValue("(O(ii))", str, valpred, index);
  1416. Py_DECREF(str);
  1417. return rv;
  1418. }
  1419. static PyMethodDef audioop_methods[] = {
  1420. { "max", audioop_max, METH_VARARGS },
  1421. { "minmax", audioop_minmax, METH_VARARGS },
  1422. { "avg", audioop_avg, METH_VARARGS },
  1423. { "maxpp", audioop_maxpp, METH_VARARGS },
  1424. { "avgpp", audioop_avgpp, METH_VARARGS },
  1425. { "rms", audioop_rms, METH_VARARGS },
  1426. { "findfit", audioop_findfit, METH_VARARGS },
  1427. { "findmax", audioop_findmax, METH_VARARGS },
  1428. { "findfactor", audioop_findfactor, METH_VARARGS },
  1429. { "cross", audioop_cross, METH_VARARGS },
  1430. { "mul", audioop_mul, METH_VARARGS },
  1431. { "add", audioop_add, METH_VARARGS },
  1432. { "bias", audioop_bias, METH_VARARGS },
  1433. { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
  1434. { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
  1435. { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
  1436. { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
  1437. { "lin2lin", audioop_lin2lin, METH_VARARGS },
  1438. { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
  1439. { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
  1440. { "tomono", audioop_tomono, METH_VARARGS },
  1441. { "tostereo", audioop_tostereo, METH_VARARGS },
  1442. { "getsample", audioop_getsample, METH_VARARGS },
  1443. { "reverse", audioop_reverse, METH_VARARGS },
  1444. { "ratecv", audioop_ratecv, METH_VARARGS },
  1445. { 0, 0 }
  1446. };
  1447. PyMODINIT_FUNC
  1448. initaudioop(void)
  1449. {
  1450. PyObject *m, *d;
  1451. m = Py_InitModule("audioop", audioop_methods);
  1452. if (m == NULL)
  1453. return;
  1454. d = PyModule_GetDict(m);
  1455. if (d == NULL)
  1456. return;
  1457. AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
  1458. if (AudioopError != NULL)
  1459. PyDict_SetItemString(d,"error",AudioopError);
  1460. }