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