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 |
| 312 | audioop_check_parameters(int len, int size) |
| 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; |
| 516 | j = 0; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | for ( j=1; j<=len1-len2; j++) { |
| 519 | aj_m1 = (double)cp1[j-1]; |
| 520 | aj_lm1 = (double)cp1[j+len2-1]; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1; |
| 523 | sum_aij_ri = _sum2(cp1+j, cp2, len2); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) |
| 526 | / sum_aij_2; |
| 527 | |
| 528 | if ( result < best_result ) { |
| 529 | best_result = result; |
| 530 | best_j = j; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 531 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2; |
| 536 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 537 | return Py_BuildValue("(nf)", best_j, factor); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /* |
| 541 | ** findfactor finds a factor f so that the energy in A-fB is minimal. |
| 542 | ** See the comment for findfit for details. |
| 543 | */ |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 544 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 545 | audioop_findfactor(PyObject *self, PyObject *args) |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | short *cp1, *cp2; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 548 | Py_ssize_t len1, len2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | double sum_ri_2, sum_aij_ri, result; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | if ( !PyArg_ParseTuple(args, "s#s#:findfactor", |
| 552 | (char**)&cp1, &len1, (char**)&cp2, &len2) ) |
| 553 | return 0; |
| 554 | if ( len1 & 1 || len2 & 1 ) { |
| 555 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
| 556 | return 0; |
| 557 | } |
| 558 | if ( len1 != len2 ) { |
| 559 | PyErr_SetString(AudioopError, "Samples should be same size"); |
| 560 | return 0; |
| 561 | } |
| 562 | len2 >>= 1; |
| 563 | sum_ri_2 = _sum2(cp2, cp2, len2); |
| 564 | sum_aij_ri = _sum2(cp1, cp2, len2); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | result = sum_aij_ri / sum_ri_2; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | return PyFloat_FromDouble(result); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
| 572 | ** findmax returns the index of the n-sized segment of the input sample |
| 573 | ** that contains the most energy. |
| 574 | */ |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 575 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 576 | audioop_findmax(PyObject *self, PyObject *args) |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | short *cp1; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 579 | Py_ssize_t len1, len2; |
| 580 | Py_ssize_t j, best_j; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 581 | double aj_m1, aj_lm1; |
| 582 | double result, best_result; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 583 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 584 | if ( !PyArg_ParseTuple(args, "s#n:findmax", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | (char**)&cp1, &len1, &len2) ) |
| 586 | return 0; |
| 587 | if ( len1 & 1 ) { |
| 588 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
| 589 | return 0; |
| 590 | } |
| 591 | len1 >>= 1; |
| 592 | |
| 593 | if ( len2 < 0 || len1 < len2 ) { |
| 594 | PyErr_SetString(AudioopError, "Input sample should be longer"); |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | result = _sum2(cp1, cp1, len2); |
| 599 | |
| 600 | best_result = result; |
| 601 | best_j = 0; |
| 602 | j = 0; |
| 603 | |
| 604 | for ( j=1; j<=len1-len2; j++) { |
| 605 | aj_m1 = (double)cp1[j-1]; |
| 606 | aj_lm1 = (double)cp1[j+len2-1]; |
| 607 | |
| 608 | result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1; |
| 609 | |
| 610 | if ( result > best_result ) { |
| 611 | best_result = result; |
| 612 | best_j = j; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 613 | } |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 616 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 617 | return PyLong_FromSsize_t(best_j); |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 620 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 621 | audioop_avgpp(PyObject *self, PyObject *args) |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | signed char *cp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 624 | Py_ssize_t len, i; |
| 625 | int size, val = 0, prevval = 0, prevextremevalid = 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | prevextreme = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | double avg = 0.0; |
| 628 | int diff, prevdiff, extremediff, nextreme = 0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) ) |
| 631 | return 0; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 632 | if (!audioop_check_parameters(len, size)) |
| 633 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | /* Compute first delta value ahead. Also automatically makes us |
| 635 | ** skip the first extreme value |
| 636 | */ |
| 637 | if ( size == 1 ) prevval = (int)*CHARP(cp, 0); |
| 638 | else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0); |
| 639 | else if ( size == 4 ) prevval = (int)*LONGP(cp, 0); |
| 640 | if ( size == 1 ) val = (int)*CHARP(cp, size); |
| 641 | else if ( size == 2 ) val = (int)*SHORTP(cp, size); |
| 642 | else if ( size == 4 ) val = (int)*LONGP(cp, size); |
| 643 | prevdiff = val - prevval; |
| 644 | |
| 645 | for ( i=size; i<len; i+= size) { |
| 646 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 647 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 648 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 649 | diff = val - prevval; |
| 650 | if ( diff*prevdiff < 0 ) { |
| 651 | /* Derivative changed sign. Compute difference to last |
| 652 | ** extreme value and remember. |
| 653 | */ |
| 654 | if ( prevextremevalid ) { |
| 655 | extremediff = prevval - prevextreme; |
| 656 | if ( extremediff < 0 ) |
| 657 | extremediff = -extremediff; |
| 658 | avg += extremediff; |
| 659 | nextreme++; |
| 660 | } |
| 661 | prevextremevalid = 1; |
| 662 | prevextreme = prevval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 663 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | prevval = val; |
| 665 | if ( diff != 0 ) |
| 666 | prevdiff = diff; |
| 667 | } |
| 668 | if ( nextreme == 0 ) |
| 669 | val = 0; |
| 670 | else |
| 671 | val = (int)(avg / (double)nextreme); |
| 672 | return PyLong_FromLong(val); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 675 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 676 | audioop_maxpp(PyObject *self, PyObject *args) |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | signed char *cp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 679 | Py_ssize_t len, i; |
| 680 | int size, val = 0, prevval = 0, prevextremevalid = 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | prevextreme = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | int max = 0; |
| 683 | int diff, prevdiff, extremediff; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 685 | if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) ) |
| 686 | return 0; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 687 | if (!audioop_check_parameters(len, size)) |
| 688 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | /* Compute first delta value ahead. Also automatically makes us |
| 690 | ** skip the first extreme value |
| 691 | */ |
| 692 | if ( size == 1 ) prevval = (int)*CHARP(cp, 0); |
| 693 | else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0); |
| 694 | else if ( size == 4 ) prevval = (int)*LONGP(cp, 0); |
| 695 | if ( size == 1 ) val = (int)*CHARP(cp, size); |
| 696 | else if ( size == 2 ) val = (int)*SHORTP(cp, size); |
| 697 | else if ( size == 4 ) val = (int)*LONGP(cp, size); |
| 698 | prevdiff = val - prevval; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | for ( i=size; i<len; i+= size) { |
| 701 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 702 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 703 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 704 | diff = val - prevval; |
| 705 | if ( diff*prevdiff < 0 ) { |
| 706 | /* Derivative changed sign. Compute difference to |
| 707 | ** last extreme value and remember. |
| 708 | */ |
| 709 | if ( prevextremevalid ) { |
| 710 | extremediff = prevval - prevextreme; |
| 711 | if ( extremediff < 0 ) |
| 712 | extremediff = -extremediff; |
| 713 | if ( extremediff > max ) |
| 714 | max = extremediff; |
| 715 | } |
| 716 | prevextremevalid = 1; |
| 717 | prevextreme = prevval; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 718 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | prevval = val; |
| 720 | if ( diff != 0 ) |
| 721 | prevdiff = diff; |
| 722 | } |
| 723 | return PyLong_FromLong(max); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 726 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 727 | audioop_cross(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | signed char *cp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 730 | Py_ssize_t len, i; |
| 731 | int size, val = 0; |
| 732 | int prevval; |
| 733 | Py_ssize_t ncross; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) ) |
| 736 | return 0; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 737 | if (!audioop_check_parameters(len, size)) |
| 738 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | ncross = -1; |
| 740 | prevval = 17; /* Anything <> 0,1 */ |
| 741 | for ( i=0; i<len; i+= size) { |
| 742 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7; |
| 743 | else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15; |
| 744 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31; |
| 745 | val = val & 1; |
| 746 | if ( val != prevval ) ncross++; |
| 747 | prevval = val; |
| 748 | } |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 749 | return PyLong_FromSsize_t(ncross); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 752 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 753 | audioop_mul(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | signed char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 756 | Py_ssize_t len, i; |
| 757 | int size, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | double factor, fval, maxval; |
| 759 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 761 | if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) ) |
| 762 | return 0; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 763 | if (!audioop_check_parameters(len, size)) |
| 764 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | |
| 766 | if ( size == 1 ) maxval = (double) 0x7f; |
| 767 | else if ( size == 2 ) maxval = (double) 0x7fff; |
| 768 | else if ( size == 4 ) maxval = (double) 0x7fffffff; |
| 769 | else { |
| 770 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | rv = PyBytes_FromStringAndSize(NULL, len); |
| 775 | if ( rv == 0 ) |
| 776 | return 0; |
| 777 | ncp = (signed char *)PyBytes_AsString(rv); |
| 778 | |
| 779 | |
| 780 | for ( i=0; i < len; i += size ) { |
| 781 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 782 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 783 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 784 | fval = (double)val*factor; |
| 785 | if ( fval > maxval ) fval = maxval; |
| 786 | else if ( fval < -maxval ) fval = -maxval; |
| 787 | val = (int)fval; |
| 788 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)val; |
| 789 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)val; |
| 790 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val; |
| 791 | } |
| 792 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 795 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 796 | audioop_tomono(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 797 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 798 | Py_buffer pcp; |
| 799 | signed char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 800 | Py_ssize_t len, i; |
| 801 | int size, val1 = 0, val2 = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | double fac1, fac2, fval, maxval; |
| 803 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | if ( !PyArg_ParseTuple(args, "s*idd:tomono", |
| 806 | &pcp, &size, &fac1, &fac2 ) ) |
| 807 | return 0; |
| 808 | cp = pcp.buf; |
| 809 | len = pcp.len; |
Mark Dickinson | cc588c1 | 2010-07-04 10:15:11 +0000 | [diff] [blame] | 810 | if (!audioop_check_parameters(len, size)) { |
| 811 | PyBuffer_Release(&pcp); |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 812 | return NULL; |
Mark Dickinson | cc588c1 | 2010-07-04 10:15:11 +0000 | [diff] [blame] | 813 | } |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 814 | if (((len / size) & 1) != 0) { |
| 815 | PyErr_SetString(AudioopError, "not a whole number of frames"); |
Mark Dickinson | cc588c1 | 2010-07-04 10:15:11 +0000 | [diff] [blame] | 816 | PyBuffer_Release(&pcp); |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 817 | return NULL; |
| 818 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | |
| 820 | if ( size == 1 ) maxval = (double) 0x7f; |
| 821 | else if ( size == 2 ) maxval = (double) 0x7fff; |
| 822 | else if ( size == 4 ) maxval = (double) 0x7fffffff; |
| 823 | else { |
| 824 | PyBuffer_Release(&pcp); |
| 825 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | rv = PyBytes_FromStringAndSize(NULL, len/2); |
Mark Dickinson | cc588c1 | 2010-07-04 10:15:11 +0000 | [diff] [blame] | 830 | if ( rv == 0 ) { |
| 831 | PyBuffer_Release(&pcp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | return 0; |
Mark Dickinson | cc588c1 | 2010-07-04 10:15:11 +0000 | [diff] [blame] | 833 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 834 | ncp = (signed char *)PyBytes_AsString(rv); |
| 835 | |
| 836 | |
| 837 | for ( i=0; i < len; i += size*2 ) { |
| 838 | if ( size == 1 ) val1 = (int)*CHARP(cp, i); |
| 839 | else if ( size == 2 ) val1 = (int)*SHORTP(cp, i); |
| 840 | else if ( size == 4 ) val1 = (int)*LONGP(cp, i); |
| 841 | if ( size == 1 ) val2 = (int)*CHARP(cp, i+1); |
| 842 | else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2); |
| 843 | else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4); |
| 844 | fval = (double)val1*fac1 + (double)val2*fac2; |
| 845 | if ( fval > maxval ) fval = maxval; |
| 846 | else if ( fval < -maxval ) fval = -maxval; |
| 847 | val1 = (int)fval; |
| 848 | if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1; |
| 849 | else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1; |
| 850 | else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1; |
| 851 | } |
| 852 | PyBuffer_Release(&pcp); |
| 853 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 856 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 857 | audioop_tostereo(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | signed char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 860 | Py_ssize_t len, i; |
| 861 | int size, val1, val2, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | double fac1, fac2, fval, maxval; |
| 863 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | if ( !PyArg_ParseTuple(args, "s#idd:tostereo", |
| 866 | &cp, &len, &size, &fac1, &fac2 ) ) |
| 867 | return 0; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 868 | if (!audioop_check_parameters(len, size)) |
| 869 | return NULL; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | if ( size == 1 ) maxval = (double) 0x7f; |
| 872 | else if ( size == 2 ) maxval = (double) 0x7fff; |
| 873 | else if ( size == 4 ) maxval = (double) 0x7fffffff; |
| 874 | else { |
| 875 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 876 | return 0; |
| 877 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 878 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 879 | if (len > PY_SSIZE_T_MAX/2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | PyErr_SetString(PyExc_MemoryError, |
| 881 | "not enough memory for output buffer"); |
| 882 | return 0; |
| 883 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 884 | |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 885 | rv = PyBytes_FromStringAndSize(NULL, len*2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | if ( rv == 0 ) |
| 887 | return 0; |
| 888 | ncp = (signed char *)PyBytes_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 889 | |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | for ( i=0; i < len; i += size ) { |
| 892 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 893 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 894 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 895 | |
| 896 | fval = (double)val*fac1; |
| 897 | if ( fval > maxval ) fval = maxval; |
| 898 | else if ( fval < -maxval ) fval = -maxval; |
| 899 | val1 = (int)fval; |
| 900 | |
| 901 | fval = (double)val*fac2; |
| 902 | if ( fval > maxval ) fval = maxval; |
| 903 | else if ( fval < -maxval ) fval = -maxval; |
| 904 | val2 = (int)fval; |
| 905 | |
| 906 | if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1; |
| 907 | else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1; |
| 908 | else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1; |
| 909 | |
| 910 | if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2; |
| 911 | else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2; |
| 912 | else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2; |
| 913 | } |
| 914 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 917 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 918 | audioop_add(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 919 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | signed char *cp1, *cp2, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 921 | Py_ssize_t len1, len2, i; |
| 922 | int size, val1 = 0, val2 = 0, maxval, newval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | if ( !PyArg_ParseTuple(args, "s#s#i:add", |
| 926 | &cp1, &len1, &cp2, &len2, &size ) ) |
| 927 | return 0; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 928 | if (!audioop_check_parameters(len1, size)) |
| 929 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | if ( len1 != len2 ) { |
| 931 | PyErr_SetString(AudioopError, "Lengths should be the same"); |
| 932 | return 0; |
| 933 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 934 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | if ( size == 1 ) maxval = 0x7f; |
| 936 | else if ( size == 2 ) maxval = 0x7fff; |
| 937 | else if ( size == 4 ) maxval = 0x7fffffff; |
| 938 | else { |
| 939 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 940 | return 0; |
| 941 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | rv = PyBytes_FromStringAndSize(NULL, len1); |
| 944 | if ( rv == 0 ) |
| 945 | return 0; |
| 946 | ncp = (signed char *)PyBytes_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | for ( i=0; i < len1; i += size ) { |
| 949 | if ( size == 1 ) val1 = (int)*CHARP(cp1, i); |
| 950 | else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i); |
| 951 | else if ( size == 4 ) val1 = (int)*LONGP(cp1, i); |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | if ( size == 1 ) val2 = (int)*CHARP(cp2, i); |
| 954 | else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i); |
| 955 | else if ( size == 4 ) val2 = (int)*LONGP(cp2, i); |
| 956 | |
| 957 | newval = val1 + val2; |
| 958 | /* truncate in case of overflow */ |
| 959 | if (newval > maxval) newval = maxval; |
| 960 | else if (newval < -maxval) newval = -maxval; |
| 961 | else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0) |
| 962 | newval = val1 > 0 ? maxval : - maxval; |
| 963 | |
| 964 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval; |
| 965 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval; |
| 966 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval; |
| 967 | } |
| 968 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 971 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 972 | audioop_bias(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | signed char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 975 | Py_ssize_t len, i; |
| 976 | int size, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | PyObject *rv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | int bias; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | if ( !PyArg_ParseTuple(args, "s#ii:bias", |
| 981 | &cp, &len, &size , &bias) ) |
| 982 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 983 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 984 | if (!audioop_check_parameters(len, size)) |
| 985 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | |
| 987 | rv = PyBytes_FromStringAndSize(NULL, len); |
| 988 | if ( rv == 0 ) |
| 989 | return 0; |
| 990 | ncp = (signed char *)PyBytes_AsString(rv); |
| 991 | |
| 992 | |
| 993 | for ( i=0; i < len; i += size ) { |
| 994 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 995 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 996 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 997 | |
| 998 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias); |
| 999 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias); |
| 1000 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias); |
| 1001 | } |
| 1002 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1005 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1006 | audioop_reverse(PyObject *self, PyObject *args) |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | signed char *cp; |
| 1009 | unsigned char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1010 | Py_ssize_t len, i, j; |
| 1011 | int size, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | PyObject *rv; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | if ( !PyArg_ParseTuple(args, "s#i:reverse", |
| 1015 | &cp, &len, &size) ) |
| 1016 | return 0; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1017 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1018 | if (!audioop_check_parameters(len, size)) |
| 1019 | return NULL; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | rv = PyBytes_FromStringAndSize(NULL, len); |
| 1022 | if ( rv == 0 ) |
| 1023 | return 0; |
| 1024 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1025 | |
| 1026 | for ( i=0; i < len; i += size ) { |
| 1027 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1028 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1029 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
| 1030 | |
| 1031 | j = len - i - size; |
| 1032 | |
| 1033 | if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8); |
| 1034 | else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val); |
| 1035 | else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16); |
| 1036 | } |
| 1037 | return rv; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1040 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1041 | audioop_lin2lin(PyObject *self, PyObject *args) |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1042 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1043 | signed char *cp; |
| 1044 | unsigned char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1045 | Py_ssize_t len, i, j; |
| 1046 | int size, size2, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | PyObject *rv; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | if ( !PyArg_ParseTuple(args, "s#ii:lin2lin", |
| 1050 | &cp, &len, &size, &size2) ) |
| 1051 | return 0; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1052 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1053 | if (!audioop_check_parameters(len, size)) |
| 1054 | return NULL; |
| 1055 | if (!audioop_check_size(size2)) |
| 1056 | return NULL; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1057 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1058 | if (len/size > PY_SSIZE_T_MAX/size2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | PyErr_SetString(PyExc_MemoryError, |
| 1060 | "not enough memory for output buffer"); |
| 1061 | return 0; |
| 1062 | } |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1063 | rv = PyBytes_FromStringAndSize(NULL, (len/size)*size2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | if ( rv == 0 ) |
| 1065 | return 0; |
| 1066 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1067 | |
| 1068 | for ( i=0, j=0; i < len; i += size, j += size2 ) { |
| 1069 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1070 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1071 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
| 1072 | |
| 1073 | if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8); |
| 1074 | else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val); |
| 1075 | else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16); |
| 1076 | } |
| 1077 | return rv; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1080 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1081 | gcd(int a, int b) |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1082 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | while (b > 0) { |
| 1084 | int tmp = a % b; |
| 1085 | a = b; |
| 1086 | b = tmp; |
| 1087 | } |
| 1088 | return a; |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1091 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1092 | audioop_ratecv(PyObject *self, PyObject *args) |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1095 | Py_ssize_t len; |
| 1096 | int size, nchannels, inrate, outrate, weightA, weightB; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | int chan, d, *prev_i, *cur_i, cur_o; |
| 1098 | PyObject *state, *samps, *str, *rv = NULL; |
| 1099 | int bytes_per_frame; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | weightA = 1; |
| 1102 | weightB = 0; |
| 1103 | if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size, |
| 1104 | &nchannels, &inrate, &outrate, &state, |
| 1105 | &weightA, &weightB)) |
| 1106 | return NULL; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1107 | if (!audioop_check_size(size)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | if (nchannels < 1) { |
| 1110 | PyErr_SetString(AudioopError, "# of channels should be >= 1"); |
| 1111 | return NULL; |
| 1112 | } |
| 1113 | bytes_per_frame = size * nchannels; |
| 1114 | if (bytes_per_frame / nchannels != size) { |
| 1115 | /* This overflow test is rigorously correct because |
| 1116 | both multiplicands are >= 1. Use the argument names |
| 1117 | from the docs for the error msg. */ |
| 1118 | PyErr_SetString(PyExc_OverflowError, |
| 1119 | "width * nchannels too big for a C int"); |
| 1120 | return NULL; |
| 1121 | } |
| 1122 | if (weightA < 1 || weightB < 0) { |
| 1123 | PyErr_SetString(AudioopError, |
| 1124 | "weightA should be >= 1, weightB should be >= 0"); |
| 1125 | return NULL; |
| 1126 | } |
| 1127 | if (len % bytes_per_frame != 0) { |
| 1128 | PyErr_SetString(AudioopError, "not a whole number of frames"); |
| 1129 | return NULL; |
| 1130 | } |
| 1131 | if (inrate <= 0 || outrate <= 0) { |
| 1132 | PyErr_SetString(AudioopError, "sampling rate not > 0"); |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | /* divide inrate and outrate by their greatest common divisor */ |
| 1136 | d = gcd(inrate, outrate); |
| 1137 | inrate /= d; |
| 1138 | outrate /= d; |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1139 | |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1140 | if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | PyErr_SetString(PyExc_MemoryError, |
| 1142 | "not enough memory for output buffer"); |
| 1143 | return 0; |
| 1144 | } |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1145 | prev_i = (int *) malloc(nchannels * sizeof(int)); |
| 1146 | cur_i = (int *) malloc(nchannels * sizeof(int)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (prev_i == NULL || cur_i == NULL) { |
| 1148 | (void) PyErr_NoMemory(); |
| 1149 | goto exit; |
| 1150 | } |
| 1151 | |
| 1152 | len /= bytes_per_frame; /* # of frames */ |
| 1153 | |
| 1154 | if (state == Py_None) { |
| 1155 | d = -outrate; |
| 1156 | for (chan = 0; chan < nchannels; chan++) |
| 1157 | prev_i[chan] = cur_i[chan] = 0; |
| 1158 | } |
| 1159 | else { |
| 1160 | if (!PyArg_ParseTuple(state, |
| 1161 | "iO!;audioop.ratecv: illegal state argument", |
| 1162 | &d, &PyTuple_Type, &samps)) |
| 1163 | goto exit; |
| 1164 | if (PyTuple_Size(samps) != nchannels) { |
| 1165 | PyErr_SetString(AudioopError, |
| 1166 | "illegal state argument"); |
| 1167 | goto exit; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1168 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | for (chan = 0; chan < nchannels; chan++) { |
| 1170 | if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan), |
| 1171 | "ii:ratecv", &prev_i[chan], |
| 1172 | &cur_i[chan])) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1173 | goto exit; |
| 1174 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | /* str <- Space for the output buffer. */ |
| 1178 | { |
| 1179 | /* There are len input frames, so we need (mathematically) |
| 1180 | ceiling(len*outrate/inrate) output frames, and each frame |
| 1181 | requires bytes_per_frame bytes. Computing this |
| 1182 | without spurious overflow is the challenge; we can |
Mark Dickinson | 393b97a | 2010-05-11 13:09:58 +0000 | [diff] [blame] | 1183 | settle for a reasonable upper bound, though, in this |
| 1184 | case ceiling(len/inrate) * outrate. */ |
| 1185 | |
| 1186 | /* compute ceiling(len/inrate) without overflow */ |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1187 | Py_ssize_t q = len > 0 ? 1 + (len - 1) / inrate : 0; |
| 1188 | if (outrate > PY_SSIZE_T_MAX / q / bytes_per_frame) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | str = NULL; |
| 1190 | else |
Mark Dickinson | 393b97a | 2010-05-11 13:09:58 +0000 | [diff] [blame] | 1191 | str = PyBytes_FromStringAndSize(NULL, |
| 1192 | q * outrate * bytes_per_frame); |
Tim Peters | 1691bd9 | 2001-12-05 06:05:07 +0000 | [diff] [blame] | 1193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | if (str == NULL) { |
| 1195 | PyErr_SetString(PyExc_MemoryError, |
| 1196 | "not enough memory for output buffer"); |
| 1197 | goto exit; |
| 1198 | } |
| 1199 | } |
| 1200 | ncp = PyBytes_AsString(str); |
| 1201 | |
| 1202 | for (;;) { |
| 1203 | while (d < 0) { |
| 1204 | if (len == 0) { |
| 1205 | samps = PyTuple_New(nchannels); |
| 1206 | if (samps == NULL) |
| 1207 | goto exit; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1208 | for (chan = 0; chan < nchannels; chan++) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | PyTuple_SetItem(samps, chan, |
| 1210 | Py_BuildValue("(ii)", |
| 1211 | prev_i[chan], |
| 1212 | cur_i[chan])); |
| 1213 | if (PyErr_Occurred()) |
| 1214 | goto exit; |
| 1215 | /* We have checked before that the length |
| 1216 | * of the string fits into int. */ |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1217 | len = (Py_ssize_t)(ncp - PyBytes_AsString(str)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | rv = PyBytes_FromStringAndSize |
| 1219 | (PyBytes_AsString(str), len); |
| 1220 | Py_DECREF(str); |
| 1221 | str = rv; |
| 1222 | if (str == NULL) |
| 1223 | goto exit; |
| 1224 | rv = Py_BuildValue("(O(iO))", str, d, samps); |
| 1225 | Py_DECREF(samps); |
| 1226 | Py_DECREF(str); |
| 1227 | goto exit; /* return rv */ |
| 1228 | } |
| 1229 | for (chan = 0; chan < nchannels; chan++) { |
| 1230 | prev_i[chan] = cur_i[chan]; |
| 1231 | if (size == 1) |
| 1232 | cur_i[chan] = ((int)*CHARP(cp, 0)) << 8; |
| 1233 | else if (size == 2) |
| 1234 | cur_i[chan] = (int)*SHORTP(cp, 0); |
| 1235 | else if (size == 4) |
| 1236 | cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16; |
| 1237 | cp += size; |
| 1238 | /* implements a simple digital filter */ |
| 1239 | cur_i[chan] = |
| 1240 | (weightA * cur_i[chan] + |
| 1241 | weightB * prev_i[chan]) / |
| 1242 | (weightA + weightB); |
| 1243 | } |
| 1244 | len--; |
| 1245 | d += outrate; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1246 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1247 | while (d >= 0) { |
| 1248 | for (chan = 0; chan < nchannels; chan++) { |
| 1249 | cur_o = (prev_i[chan] * d + |
| 1250 | cur_i[chan] * (outrate - d)) / |
| 1251 | outrate; |
| 1252 | if (size == 1) |
| 1253 | *CHARP(ncp, 0) = (signed char)(cur_o >> 8); |
| 1254 | else if (size == 2) |
| 1255 | *SHORTP(ncp, 0) = (short)(cur_o); |
| 1256 | else if (size == 4) |
| 1257 | *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16); |
| 1258 | ncp += size; |
| 1259 | } |
| 1260 | d -= inrate; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1261 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | } |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame] | 1263 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | if (prev_i != NULL) |
| 1265 | free(prev_i); |
| 1266 | if (cur_i != NULL) |
| 1267 | free(cur_i); |
| 1268 | return rv; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1269 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1270 | |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1271 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1272 | audioop_lin2ulaw(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1273 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | signed char *cp; |
| 1275 | unsigned char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1276 | Py_ssize_t len, i; |
| 1277 | int size, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw", |
| 1281 | &cp, &len, &size) ) |
| 1282 | return 0 ; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1283 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1284 | if (!audioop_check_parameters(len, size)) |
| 1285 | return NULL; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | rv = PyBytes_FromStringAndSize(NULL, len/size); |
| 1288 | if ( rv == 0 ) |
| 1289 | return 0; |
| 1290 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1291 | |
| 1292 | for ( i=0; i < len; i += size ) { |
| 1293 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1294 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1295 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
| 1296 | |
| 1297 | *ncp++ = st_14linear2ulaw(val); |
| 1298 | } |
| 1299 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1302 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1303 | audioop_ulaw2lin(PyObject *self, PyObject *args) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | unsigned char *cp; |
| 1306 | unsigned char cval; |
| 1307 | signed char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1308 | Py_ssize_t len, i; |
| 1309 | int size, val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1310 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin", |
| 1313 | &cp, &len, &size) ) |
| 1314 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1315 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1316 | if (!audioop_check_parameters(len, size)) |
| 1317 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1319 | if (len > PY_SSIZE_T_MAX/size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | PyErr_SetString(PyExc_MemoryError, |
| 1321 | "not enough memory for output buffer"); |
| 1322 | return 0; |
| 1323 | } |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1324 | rv = PyBytes_FromStringAndSize(NULL, len*size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | if ( rv == 0 ) |
| 1326 | return 0; |
| 1327 | ncp = (signed char *)PyBytes_AsString(rv); |
| 1328 | |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1329 | for ( i=0; i < len*size; i += size ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | cval = *cp++; |
| 1331 | val = st_ulaw2linear16(cval); |
| 1332 | |
| 1333 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8); |
| 1334 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val); |
| 1335 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16); |
| 1336 | } |
| 1337 | return rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | static PyObject * |
| 1341 | audioop_lin2alaw(PyObject *self, PyObject *args) |
| 1342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | signed char *cp; |
| 1344 | unsigned char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1345 | Py_ssize_t len, i; |
| 1346 | int size, val = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | PyObject *rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | if ( !PyArg_ParseTuple(args, "s#i:lin2alaw", |
| 1350 | &cp, &len, &size) ) |
| 1351 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1352 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1353 | if (!audioop_check_parameters(len, size)) |
| 1354 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1355 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | rv = PyBytes_FromStringAndSize(NULL, len/size); |
| 1357 | if ( rv == 0 ) |
| 1358 | return 0; |
| 1359 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1360 | |
| 1361 | for ( i=0; i < len; i += size ) { |
| 1362 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1363 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1364 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
| 1365 | |
| 1366 | *ncp++ = st_linear2alaw(val); |
| 1367 | } |
| 1368 | return rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | static PyObject * |
| 1372 | audioop_alaw2lin(PyObject *self, PyObject *args) |
| 1373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | unsigned char *cp; |
| 1375 | unsigned char cval; |
| 1376 | signed char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1377 | Py_ssize_t len, i; |
| 1378 | int size, val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | PyObject *rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | if ( !PyArg_ParseTuple(args, "s#i:alaw2lin", |
| 1382 | &cp, &len, &size) ) |
| 1383 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1384 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1385 | if (!audioop_check_parameters(len, size)) |
| 1386 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1388 | if (len > PY_SSIZE_T_MAX/size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | PyErr_SetString(PyExc_MemoryError, |
| 1390 | "not enough memory for output buffer"); |
| 1391 | return 0; |
| 1392 | } |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1393 | rv = PyBytes_FromStringAndSize(NULL, len*size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | if ( rv == 0 ) |
| 1395 | return 0; |
| 1396 | ncp = (signed char *)PyBytes_AsString(rv); |
| 1397 | |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1398 | for ( i=0; i < len*size; i += size ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | cval = *cp++; |
| 1400 | val = st_alaw2linear16(cval); |
| 1401 | |
| 1402 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8); |
| 1403 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val); |
| 1404 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16); |
| 1405 | } |
| 1406 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1409 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1410 | audioop_lin2adpcm(PyObject *self, PyObject *args) |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | signed char *cp; |
| 1413 | signed char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1414 | Py_ssize_t len, i; |
| 1415 | int size, val = 0, step, valpred, delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | index, sign, vpdiff, diff; |
| 1417 | PyObject *rv, *state, *str; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1418 | int outputbuffer = 0, bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm", |
| 1421 | &cp, &len, &size, &state) ) |
| 1422 | return 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1423 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1424 | if (!audioop_check_parameters(len, size)) |
| 1425 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | |
| 1427 | str = PyBytes_FromStringAndSize(NULL, len/(size*2)); |
| 1428 | if ( str == 0 ) |
| 1429 | return 0; |
| 1430 | ncp = (signed char *)PyBytes_AsString(str); |
| 1431 | |
| 1432 | /* Decode state, should have (value, step) */ |
| 1433 | if ( state == Py_None ) { |
| 1434 | /* First time, it seems. Set defaults */ |
| 1435 | valpred = 0; |
| 1436 | step = 7; |
| 1437 | index = 0; |
| 1438 | } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) ) |
| 1439 | return 0; |
| 1440 | |
| 1441 | step = stepsizeTable[index]; |
| 1442 | bufferstep = 1; |
| 1443 | |
| 1444 | for ( i=0; i < len; i += size ) { |
| 1445 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1446 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1447 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
| 1448 | |
| 1449 | /* Step 1 - compute difference with previous value */ |
| 1450 | diff = val - valpred; |
| 1451 | sign = (diff < 0) ? 8 : 0; |
| 1452 | if ( sign ) diff = (-diff); |
| 1453 | |
| 1454 | /* Step 2 - Divide and clamp */ |
| 1455 | /* Note: |
| 1456 | ** This code *approximately* computes: |
| 1457 | ** delta = diff*4/step; |
| 1458 | ** vpdiff = (delta+0.5)*step/4; |
| 1459 | ** but in shift step bits are dropped. The net result of this |
| 1460 | ** is that even if you have fast mul/div hardware you cannot |
| 1461 | ** put it to good use since the fixup would be too expensive. |
| 1462 | */ |
| 1463 | delta = 0; |
| 1464 | vpdiff = (step >> 3); |
| 1465 | |
| 1466 | if ( diff >= step ) { |
| 1467 | delta = 4; |
| 1468 | diff -= step; |
| 1469 | vpdiff += step; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1470 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | step >>= 1; |
| 1472 | if ( diff >= step ) { |
| 1473 | delta |= 2; |
| 1474 | diff -= step; |
| 1475 | vpdiff += step; |
| 1476 | } |
| 1477 | step >>= 1; |
| 1478 | if ( diff >= step ) { |
| 1479 | delta |= 1; |
| 1480 | vpdiff += step; |
| 1481 | } |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | /* Step 3 - Update previous value */ |
| 1484 | if ( sign ) |
| 1485 | valpred -= vpdiff; |
| 1486 | else |
| 1487 | valpred += vpdiff; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | /* Step 4 - Clamp previous value to 16 bits */ |
| 1490 | if ( valpred > 32767 ) |
| 1491 | valpred = 32767; |
| 1492 | else if ( valpred < -32768 ) |
| 1493 | valpred = -32768; |
| 1494 | |
| 1495 | /* Step 5 - Assemble value, update index and step values */ |
| 1496 | delta |= sign; |
| 1497 | |
| 1498 | index += indexTable[delta]; |
| 1499 | if ( index < 0 ) index = 0; |
| 1500 | if ( index > 88 ) index = 88; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1501 | step = stepsizeTable[index]; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | /* Step 6 - Output value */ |
| 1504 | if ( bufferstep ) { |
| 1505 | outputbuffer = (delta << 4) & 0xf0; |
| 1506 | } else { |
| 1507 | *ncp++ = (delta & 0x0f) | outputbuffer; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1508 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | bufferstep = !bufferstep; |
| 1510 | } |
| 1511 | rv = Py_BuildValue("(O(ii))", str, valpred, index); |
| 1512 | Py_DECREF(str); |
| 1513 | return rv; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1516 | static PyObject * |
Peter Schneider-Kamp | 8bc8f0d | 2000-07-10 17:15:07 +0000 | [diff] [blame] | 1517 | audioop_adpcm2lin(PyObject *self, PyObject *args) |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | signed char *cp; |
| 1520 | signed char *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1521 | Py_ssize_t len, i; |
| 1522 | int size, valpred, step, delta, index, sign, vpdiff; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | PyObject *rv, *str, *state; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1524 | int inputbuffer = 0, bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin", |
| 1527 | &cp, &len, &size, &state) ) |
| 1528 | return 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1529 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1530 | if (!audioop_check_parameters(len, size)) |
| 1531 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | |
| 1533 | /* Decode state, should have (value, step) */ |
| 1534 | if ( state == Py_None ) { |
| 1535 | /* First time, it seems. Set defaults */ |
| 1536 | valpred = 0; |
| 1537 | step = 7; |
| 1538 | index = 0; |
| 1539 | } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) ) |
| 1540 | return 0; |
| 1541 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1542 | if (len > (PY_SSIZE_T_MAX/2)/size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | PyErr_SetString(PyExc_MemoryError, |
| 1544 | "not enough memory for output buffer"); |
| 1545 | return 0; |
| 1546 | } |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1547 | str = PyBytes_FromStringAndSize(NULL, len*size*2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | if ( str == 0 ) |
| 1549 | return 0; |
| 1550 | ncp = (signed char *)PyBytes_AsString(str); |
| 1551 | |
| 1552 | step = stepsizeTable[index]; |
| 1553 | bufferstep = 0; |
| 1554 | |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1555 | for ( i=0; i < len*size*2; i += size ) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | /* Step 1 - get the delta value and compute next index */ |
| 1557 | if ( bufferstep ) { |
| 1558 | delta = inputbuffer & 0xf; |
| 1559 | } else { |
| 1560 | inputbuffer = *cp++; |
| 1561 | delta = (inputbuffer >> 4) & 0xf; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1562 | } |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | bufferstep = !bufferstep; |
| 1565 | |
| 1566 | /* Step 2 - Find new index value (for later) */ |
| 1567 | index += indexTable[delta]; |
| 1568 | if ( index < 0 ) index = 0; |
| 1569 | if ( index > 88 ) index = 88; |
| 1570 | |
| 1571 | /* Step 3 - Separate sign and magnitude */ |
| 1572 | sign = delta & 8; |
| 1573 | delta = delta & 7; |
| 1574 | |
| 1575 | /* Step 4 - Compute difference and new predicted value */ |
| 1576 | /* |
| 1577 | ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment |
| 1578 | ** in adpcm_coder. |
| 1579 | */ |
| 1580 | vpdiff = step >> 3; |
| 1581 | if ( delta & 4 ) vpdiff += step; |
| 1582 | if ( delta & 2 ) vpdiff += step>>1; |
| 1583 | if ( delta & 1 ) vpdiff += step>>2; |
| 1584 | |
| 1585 | if ( sign ) |
| 1586 | valpred -= vpdiff; |
| 1587 | else |
| 1588 | valpred += vpdiff; |
| 1589 | |
| 1590 | /* Step 5 - clamp output value */ |
| 1591 | if ( valpred > 32767 ) |
| 1592 | valpred = 32767; |
| 1593 | else if ( valpred < -32768 ) |
| 1594 | valpred = -32768; |
| 1595 | |
| 1596 | /* Step 6 - Update step value */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1597 | step = stepsizeTable[index]; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | /* Step 6 - Output value */ |
| 1600 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8); |
| 1601 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred); |
| 1602 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16); |
| 1603 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | rv = Py_BuildValue("(O(ii))", str, valpred, index); |
| 1606 | Py_DECREF(str); |
| 1607 | return rv; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1610 | static PyMethodDef audioop_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | { "max", audioop_max, METH_VARARGS }, |
| 1612 | { "minmax", audioop_minmax, METH_VARARGS }, |
| 1613 | { "avg", audioop_avg, METH_VARARGS }, |
| 1614 | { "maxpp", audioop_maxpp, METH_VARARGS }, |
| 1615 | { "avgpp", audioop_avgpp, METH_VARARGS }, |
| 1616 | { "rms", audioop_rms, METH_VARARGS }, |
| 1617 | { "findfit", audioop_findfit, METH_VARARGS }, |
| 1618 | { "findmax", audioop_findmax, METH_VARARGS }, |
| 1619 | { "findfactor", audioop_findfactor, METH_VARARGS }, |
| 1620 | { "cross", audioop_cross, METH_VARARGS }, |
| 1621 | { "mul", audioop_mul, METH_VARARGS }, |
| 1622 | { "add", audioop_add, METH_VARARGS }, |
| 1623 | { "bias", audioop_bias, METH_VARARGS }, |
| 1624 | { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS }, |
| 1625 | { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS }, |
| 1626 | { "alaw2lin", audioop_alaw2lin, METH_VARARGS }, |
| 1627 | { "lin2alaw", audioop_lin2alaw, METH_VARARGS }, |
| 1628 | { "lin2lin", audioop_lin2lin, METH_VARARGS }, |
| 1629 | { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS }, |
| 1630 | { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS }, |
| 1631 | { "tomono", audioop_tomono, METH_VARARGS }, |
| 1632 | { "tostereo", audioop_tostereo, METH_VARARGS }, |
| 1633 | { "getsample", audioop_getsample, METH_VARARGS }, |
| 1634 | { "reverse", audioop_reverse, METH_VARARGS }, |
| 1635 | { "ratecv", audioop_ratecv, METH_VARARGS }, |
| 1636 | { 0, 0 } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1637 | }; |
| 1638 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1639 | |
| 1640 | static struct PyModuleDef audioopmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | PyModuleDef_HEAD_INIT, |
| 1642 | "audioop", |
| 1643 | NULL, |
| 1644 | -1, |
| 1645 | audioop_methods, |
| 1646 | NULL, |
| 1647 | NULL, |
| 1648 | NULL, |
| 1649 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1650 | }; |
| 1651 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1652 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1653 | PyInit_audioop(void) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1654 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | PyObject *m, *d; |
| 1656 | m = PyModule_Create(&audioopmodule); |
| 1657 | if (m == NULL) |
| 1658 | return NULL; |
| 1659 | d = PyModule_GetDict(m); |
| 1660 | if (d == NULL) |
| 1661 | return NULL; |
| 1662 | AudioopError = PyErr_NewException("audioop.error", NULL, NULL); |
| 1663 | if (AudioopError != NULL) |
| 1664 | PyDict_SetItemString(d,"error",AudioopError); |
| 1665 | return m; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1666 | } |