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