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