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