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 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 393 | /*[clinic input] |
| 394 | output preset file |
| 395 | module audioop |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 396 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 397 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5619f935f269199a]*/ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 398 | |
| 399 | /*[clinic input] |
| 400 | audioop.getsample |
| 401 | |
| 402 | fragment: Py_buffer |
| 403 | width: int |
| 404 | index: Py_ssize_t |
| 405 | / |
| 406 | |
| 407 | Return the value of sample index from the fragment. |
| 408 | [clinic start generated code]*/ |
| 409 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 410 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 411 | audioop_getsample_impl(PyModuleDef *module, Py_buffer *fragment, int width, Py_ssize_t index) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 412 | /*[clinic end generated code: output=f4482497e6f6e78f input=88edbe2871393549]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 413 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 414 | int val; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 415 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 416 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 417 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 418 | if (index < 0 || index >= fragment->len/width) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | PyErr_SetString(AudioopError, "Index out of range"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 420 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 422 | val = GETRAWSAMPLE(width, fragment->buf, index*width); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 423 | return PyLong_FromLong(val); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 426 | /*[clinic input] |
| 427 | audioop.max |
| 428 | |
| 429 | fragment: Py_buffer |
| 430 | width: int |
| 431 | / |
| 432 | |
| 433 | Return the maximum of the absolute value of all samples in a fragment. |
| 434 | [clinic start generated code]*/ |
| 435 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 436 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 437 | audioop_max_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 438 | /*[clinic end generated code: output=85047ee1001f2305 input=32bea5ea0ac8c223]*/ |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 439 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 440 | Py_ssize_t i; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 441 | unsigned int absval, max = 0; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 442 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 443 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 444 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 445 | for (i = 0; i < fragment->len; i += width) { |
| 446 | int val = GETRAWSAMPLE(width, fragment->buf, i); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 447 | if (val < 0) absval = (-val); |
| 448 | else absval = val; |
| 449 | if (absval > max) max = absval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | } |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 451 | return PyLong_FromUnsignedLong(max); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 454 | /*[clinic input] |
| 455 | audioop.minmax |
| 456 | |
| 457 | fragment: Py_buffer |
| 458 | width: int |
| 459 | / |
| 460 | |
| 461 | Return the minimum and maximum values of all samples in the sound fragment. |
| 462 | [clinic start generated code]*/ |
| 463 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 464 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 465 | audioop_minmax_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 466 | /*[clinic end generated code: output=ae8f5513c64fd569 input=89848e9b927a0696]*/ |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 467 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 468 | Py_ssize_t i; |
Tim Golden | fa6ab0f | 2013-10-31 10:25:47 +0000 | [diff] [blame] | 469 | /* -1 trick below is needed on Windows to support -0x80000000 without |
| 470 | a warning */ |
| 471 | int min = 0x7fffffff, max = -0x7FFFFFFF-1; |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 472 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 473 | if (!audioop_check_parameters(fragment->len, width)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 475 | for (i = 0; i < fragment->len; i += width) { |
| 476 | int val = GETRAWSAMPLE(width, fragment->buf, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | if (val > max) max = val; |
| 478 | if (val < min) min = val; |
| 479 | } |
| 480 | return Py_BuildValue("(ii)", min, max); |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 483 | /*[clinic input] |
| 484 | audioop.avg |
| 485 | |
| 486 | fragment: Py_buffer |
| 487 | width: int |
| 488 | / |
| 489 | |
| 490 | Return the average over all samples in the fragment. |
| 491 | [clinic start generated code]*/ |
| 492 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 493 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 494 | audioop_avg_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 495 | /*[clinic end generated code: output=7fccd645c95f4860 input=1114493c7611334d]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 496 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 497 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 498 | int avg; |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 499 | double sum = 0.0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 500 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 501 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 502 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 503 | for (i = 0; i < fragment->len; i += width) |
| 504 | sum += GETRAWSAMPLE(width, fragment->buf, i); |
| 505 | if (fragment->len == 0) |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 506 | avg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | else |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 508 | avg = (int)floor(sum / (double)(fragment->len/width)); |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 509 | return PyLong_FromLong(avg); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 512 | /*[clinic input] |
| 513 | audioop.rms |
| 514 | |
| 515 | fragment: Py_buffer |
| 516 | width: int |
| 517 | / |
| 518 | |
| 519 | Return the root-mean-square of the fragment, i.e. sqrt(sum(S_i^2)/n). |
| 520 | [clinic start generated code]*/ |
| 521 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 522 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 523 | audioop_rms_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 524 | /*[clinic end generated code: output=7b398702c81b709d input=4cc57c6c94219d78]*/ |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 525 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 526 | Py_ssize_t i; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 527 | unsigned int res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | double sum_squares = 0.0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 529 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 530 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 531 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 532 | for (i = 0; i < fragment->len; i += width) { |
| 533 | double val = GETRAWSAMPLE(width, fragment->buf, i); |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 534 | sum_squares += val*val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 536 | if (fragment->len == 0) |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 537 | res = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | else |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 539 | res = (unsigned int)sqrt(sum_squares / (double)(fragment->len/width)); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 540 | return PyLong_FromUnsignedLong(res); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 543 | 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] | 544 | { |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 545 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | double sum = 0.0; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | for( i=0; i<len; i++) { |
| 549 | sum = sum + (double)a[i]*(double)b[i]; |
| 550 | } |
| 551 | return sum; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* |
| 555 | ** Findfit tries to locate a sample within another sample. Its main use |
| 556 | ** is in echo-cancellation (to find the feedback of the output signal in |
| 557 | ** the input signal). |
| 558 | ** The method used is as follows: |
| 559 | ** |
| 560 | ** let R be the reference signal (length n) and A the input signal (length N) |
| 561 | ** with N > n, and let all sums be over i from 0 to n-1. |
| 562 | ** |
| 563 | ** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A |
| 564 | ** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This |
| 565 | ** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2). |
| 566 | ** |
| 567 | ** Next, we compute the relative distance between the original signal and |
| 568 | ** the modified signal and minimize that over j: |
| 569 | ** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 ) => |
| 570 | ** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 ) |
| 571 | ** |
| 572 | ** In the code variables correspond as follows: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 573 | ** cp1 A |
| 574 | ** cp2 R |
| 575 | ** len1 N |
| 576 | ** len2 n |
| 577 | ** aj_m1 A[j-1] |
| 578 | ** aj_lm1 A[j+n-1] |
| 579 | ** sum_ri_2 sum(R[i]^2) |
| 580 | ** sum_aij_2 sum(A[i+j]^2) |
| 581 | ** sum_aij_ri sum(A[i+j]R[i]) |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 582 | ** |
| 583 | ** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri |
| 584 | ** is completely recalculated each step. |
| 585 | */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 586 | /*[clinic input] |
| 587 | audioop.findfit |
| 588 | |
| 589 | fragment: Py_buffer |
| 590 | reference: Py_buffer |
| 591 | / |
| 592 | |
| 593 | Try to match reference as well as possible to a portion of fragment. |
| 594 | [clinic start generated code]*/ |
| 595 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 596 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 597 | audioop_findfit_impl(PyModuleDef *module, Py_buffer *fragment, Py_buffer *reference) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 598 | /*[clinic end generated code: output=505fd04d4244db31 input=62c305605e183c9a]*/ |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 599 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 600 | const short *cp1, *cp2; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 601 | Py_ssize_t len1, len2; |
| 602 | Py_ssize_t j, best_j; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | double aj_m1, aj_lm1; |
| 604 | 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] | 605 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 606 | if (fragment->len & 1 || reference->len & 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 608 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 610 | cp1 = (const short *)fragment->buf; |
| 611 | len1 = fragment->len >> 1; |
| 612 | cp2 = (const short *)reference->buf; |
| 613 | len2 = reference->len >> 1; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 614 | |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 615 | if (len1 < len2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | PyErr_SetString(AudioopError, "First sample should be longer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 617 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | } |
| 619 | sum_ri_2 = _sum2(cp2, cp2, len2); |
| 620 | sum_aij_2 = _sum2(cp1, cp1, len2); |
| 621 | sum_aij_ri = _sum2(cp1, cp2, len2); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | 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] | 624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | best_result = result; |
| 626 | best_j = 0; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | for ( j=1; j<=len1-len2; j++) { |
| 629 | aj_m1 = (double)cp1[j-1]; |
| 630 | aj_lm1 = (double)cp1[j+len2-1]; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1; |
| 633 | sum_aij_ri = _sum2(cp1+j, cp2, len2); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) |
| 636 | / sum_aij_2; |
| 637 | |
| 638 | if ( result < best_result ) { |
| 639 | best_result = result; |
| 640 | best_j = j; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 641 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2; |
| 646 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 647 | return Py_BuildValue("(nf)", best_j, factor); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
| 651 | ** findfactor finds a factor f so that the energy in A-fB is minimal. |
| 652 | ** See the comment for findfit for details. |
| 653 | */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 654 | /*[clinic input] |
| 655 | audioop.findfactor |
| 656 | |
| 657 | fragment: Py_buffer |
| 658 | reference: Py_buffer |
| 659 | / |
| 660 | |
| 661 | Return a factor F such that rms(add(fragment, mul(reference, -F))) is minimal. |
| 662 | [clinic start generated code]*/ |
| 663 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 664 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 665 | audioop_findfactor_impl(PyModuleDef *module, Py_buffer *fragment, Py_buffer *reference) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 666 | /*[clinic end generated code: output=ddf35a1e57575ce4 input=816680301d012b21]*/ |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 667 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 668 | const short *cp1, *cp2; |
| 669 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | double sum_ri_2, sum_aij_ri, result; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 671 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 672 | if (fragment->len & 1 || reference->len & 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 674 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 676 | if (fragment->len != reference->len) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | PyErr_SetString(AudioopError, "Samples should be same size"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 678 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 680 | cp1 = (const short *)fragment->buf; |
| 681 | cp2 = (const short *)reference->buf; |
| 682 | len = fragment->len >> 1; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 683 | sum_ri_2 = _sum2(cp2, cp2, len); |
| 684 | sum_aij_ri = _sum2(cp1, cp2, len); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 686 | result = sum_aij_ri / sum_ri_2; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | return PyFloat_FromDouble(result); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* |
| 692 | ** findmax returns the index of the n-sized segment of the input sample |
| 693 | ** that contains the most energy. |
| 694 | */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 695 | /*[clinic input] |
| 696 | audioop.findmax |
| 697 | |
| 698 | fragment: Py_buffer |
| 699 | length: Py_ssize_t |
| 700 | / |
| 701 | |
| 702 | Search fragment for a slice of specified number of samples with maximum energy. |
| 703 | [clinic start generated code]*/ |
| 704 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 705 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 706 | audioop_findmax_impl(PyModuleDef *module, Py_buffer *fragment, Py_ssize_t length) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 707 | /*[clinic end generated code: output=21d0c2a1e5655134 input=2f304801ed42383c]*/ |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 708 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 709 | const short *cp1; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 710 | Py_ssize_t len1; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 711 | Py_ssize_t j, best_j; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 712 | double aj_m1, aj_lm1; |
| 713 | double result, best_result; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 714 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 715 | if (fragment->len & 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 717 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 719 | cp1 = (const short *)fragment->buf; |
| 720 | len1 = fragment->len >> 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 721 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 722 | if (length < 0 || len1 < length) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | PyErr_SetString(AudioopError, "Input sample should be longer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 724 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 727 | result = _sum2(cp1, cp1, length); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 728 | |
| 729 | best_result = result; |
| 730 | best_j = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 732 | for ( j=1; j<=len1-length; j++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 733 | aj_m1 = (double)cp1[j-1]; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 734 | aj_lm1 = (double)cp1[j+length-1]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | |
| 736 | result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1; |
| 737 | |
| 738 | if ( result > best_result ) { |
| 739 | best_result = result; |
| 740 | best_j = j; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 741 | } |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 744 | |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 745 | return PyLong_FromSsize_t(best_j); |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 748 | /*[clinic input] |
| 749 | audioop.avgpp |
| 750 | |
| 751 | fragment: Py_buffer |
| 752 | width: int |
| 753 | / |
| 754 | |
| 755 | Return the average peak-peak value over all samples in the fragment. |
| 756 | [clinic start generated code]*/ |
| 757 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 758 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 759 | audioop_avgpp_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 760 | /*[clinic end generated code: output=06c8380fd6e34207 input=0b3cceeae420a7d9]*/ |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 761 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 762 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 763 | int prevval, prevextremevalid = 0, prevextreme = 0; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 764 | double sum = 0.0; |
| 765 | unsigned int avg; |
| 766 | int diff, prevdiff, nextreme = 0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 767 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 768 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 769 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 770 | if (fragment->len <= width) |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 771 | return PyLong_FromLong(0); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 772 | prevval = GETRAWSAMPLE(width, fragment->buf, 0); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 773 | prevdiff = 17; /* Anything != 0, 1 */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 774 | for (i = width; i < fragment->len; i += width) { |
| 775 | int val = GETRAWSAMPLE(width, fragment->buf, i); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 776 | if (val != prevval) { |
| 777 | diff = val < prevval; |
| 778 | if (prevdiff == !diff) { |
| 779 | /* Derivative changed sign. Compute difference to last |
| 780 | ** extreme value and remember. |
| 781 | */ |
| 782 | if (prevextremevalid) { |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 783 | if (prevval < prevextreme) |
| 784 | sum += (double)((unsigned int)prevextreme - |
| 785 | (unsigned int)prevval); |
| 786 | else |
| 787 | sum += (double)((unsigned int)prevval - |
| 788 | (unsigned int)prevextreme); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 789 | nextreme++; |
| 790 | } |
| 791 | prevextremevalid = 1; |
| 792 | prevextreme = prevval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | } |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 794 | prevval = val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | prevdiff = diff; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 796 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | } |
| 798 | if ( nextreme == 0 ) |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 799 | avg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | else |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 801 | avg = (unsigned int)(sum / (double)nextreme); |
| 802 | return PyLong_FromUnsignedLong(avg); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 805 | /*[clinic input] |
| 806 | audioop.maxpp |
| 807 | |
| 808 | fragment: Py_buffer |
| 809 | width: int |
| 810 | / |
| 811 | |
| 812 | Return the maximum peak-peak value in the sound fragment. |
| 813 | [clinic start generated code]*/ |
| 814 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 815 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 816 | audioop_maxpp_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 817 | /*[clinic end generated code: output=c300c0bd7e8535c0 input=671a13e1518f80a1]*/ |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 818 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 819 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 820 | int prevval, prevextremevalid = 0, prevextreme = 0; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 821 | unsigned int max = 0, extremediff; |
| 822 | int diff, prevdiff; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 823 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 824 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 825 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 826 | if (fragment->len <= width) |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 827 | return PyLong_FromLong(0); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 828 | prevval = GETRAWSAMPLE(width, fragment->buf, 0); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 829 | prevdiff = 17; /* Anything != 0, 1 */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 830 | for (i = width; i < fragment->len; i += width) { |
| 831 | int val = GETRAWSAMPLE(width, fragment->buf, i); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 832 | if (val != prevval) { |
| 833 | diff = val < prevval; |
| 834 | if (prevdiff == !diff) { |
| 835 | /* Derivative changed sign. Compute difference to |
| 836 | ** last extreme value and remember. |
| 837 | */ |
| 838 | if (prevextremevalid) { |
| 839 | if (prevval < prevextreme) |
| 840 | extremediff = (unsigned int)prevextreme - |
| 841 | (unsigned int)prevval; |
| 842 | else |
| 843 | extremediff = (unsigned int)prevval - |
| 844 | (unsigned int)prevextreme; |
| 845 | if ( extremediff > max ) |
| 846 | max = extremediff; |
| 847 | } |
| 848 | prevextremevalid = 1; |
| 849 | prevextreme = prevval; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | } |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 851 | prevval = val; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | prevdiff = diff; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 853 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | } |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 855 | return PyLong_FromUnsignedLong(max); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 858 | /*[clinic input] |
| 859 | audioop.cross |
| 860 | |
| 861 | fragment: Py_buffer |
| 862 | width: int |
| 863 | / |
| 864 | |
| 865 | Return the number of zero crossings in the fragment passed as an argument. |
| 866 | [clinic start generated code]*/ |
| 867 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 868 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 869 | audioop_cross_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 870 | /*[clinic end generated code: output=99e6572d7d7cdbf1 input=b1b3f15b83f6b41a]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 871 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 872 | Py_ssize_t i; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 873 | int prevval; |
| 874 | Py_ssize_t ncross; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 875 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 876 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 877 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | ncross = -1; |
| 879 | prevval = 17; /* Anything <> 0,1 */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 880 | for (i = 0; i < fragment->len; i += width) { |
| 881 | int val = GETRAWSAMPLE(width, fragment->buf, i) < 0; |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 882 | if (val != prevval) ncross++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | prevval = val; |
| 884 | } |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 885 | return PyLong_FromSsize_t(ncross); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 888 | /*[clinic input] |
| 889 | audioop.mul |
| 890 | |
| 891 | fragment: Py_buffer |
| 892 | width: int |
| 893 | factor: double |
| 894 | / |
| 895 | |
| 896 | Return a fragment that has all samples in the original fragment multiplied by the floating-point value factor. |
| 897 | [clinic start generated code]*/ |
| 898 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 899 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 900 | audioop_mul_impl(PyModuleDef *module, Py_buffer *fragment, int width, double factor) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 901 | /*[clinic end generated code: output=a697ebbd5852d38f input=c726667baa157d3c]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 902 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 903 | signed char *ncp; |
| 904 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 905 | double maxval, minval; |
| 906 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 907 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 908 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 909 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 911 | maxval = (double) maxvals[width]; |
| 912 | minval = (double) minvals[width]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 914 | rv = PyBytes_FromStringAndSize(NULL, fragment->len); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 915 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 916 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | ncp = (signed char *)PyBytes_AsString(rv); |
| 918 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 919 | for (i = 0; i < fragment->len; i += width) { |
| 920 | double val = GETRAWSAMPLE(width, fragment->buf, i); |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 921 | val *= factor; |
| 922 | val = floor(fbound(val, minval, maxval)); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 923 | SETRAWSAMPLE(width, ncp, i, (int)val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | } |
| 925 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 928 | /*[clinic input] |
| 929 | audioop.tomono |
| 930 | |
| 931 | fragment: Py_buffer |
| 932 | width: int |
| 933 | lfactor: double |
| 934 | rfactor: double |
| 935 | / |
| 936 | |
| 937 | Convert a stereo fragment to a mono fragment. |
| 938 | [clinic start generated code]*/ |
| 939 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 940 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 941 | audioop_tomono_impl(PyModuleDef *module, Py_buffer *fragment, int width, double lfactor, double rfactor) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 942 | /*[clinic end generated code: output=436e7710521661dd input=c4ec949b3f4dddfa]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 943 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | signed char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 945 | Py_ssize_t len, i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 946 | double maxval, minval; |
| 947 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 948 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 949 | cp = fragment->buf; |
| 950 | len = fragment->len; |
| 951 | if (!audioop_check_parameters(len, width)) |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 952 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 953 | if (((len / width) & 1) != 0) { |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 954 | PyErr_SetString(AudioopError, "not a whole number of frames"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 955 | return NULL; |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 956 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 958 | maxval = (double) maxvals[width]; |
| 959 | minval = (double) minvals[width]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | |
| 961 | rv = PyBytes_FromStringAndSize(NULL, len/2); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 962 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 963 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | ncp = (signed char *)PyBytes_AsString(rv); |
| 965 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 966 | for (i = 0; i < len; i += width*2) { |
| 967 | double val1 = GETRAWSAMPLE(width, cp, i); |
| 968 | double val2 = GETRAWSAMPLE(width, cp, i + width); |
| 969 | double val = val1*lfactor + val2*rfactor; |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 970 | val = floor(fbound(val, minval, maxval)); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 971 | SETRAWSAMPLE(width, ncp, i/2, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 976 | /*[clinic input] |
| 977 | audioop.tostereo |
| 978 | |
| 979 | fragment: Py_buffer |
| 980 | width: int |
| 981 | lfactor: double |
| 982 | rfactor: double |
| 983 | / |
| 984 | |
| 985 | Generate a stereo fragment from a mono fragment. |
| 986 | [clinic start generated code]*/ |
| 987 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 988 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 989 | audioop_tostereo_impl(PyModuleDef *module, Py_buffer *fragment, int width, double lfactor, double rfactor) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 990 | /*[clinic end generated code: output=6ff50681c87f4c1c input=27b6395ebfdff37a]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 991 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 992 | signed char *ncp; |
| 993 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 994 | double maxval, minval; |
| 995 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 996 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 997 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 998 | return NULL; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 999 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1000 | maxval = (double) maxvals[width]; |
| 1001 | minval = (double) minvals[width]; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1002 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1003 | if (fragment->len > PY_SSIZE_T_MAX/2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | PyErr_SetString(PyExc_MemoryError, |
| 1005 | "not enough memory for output buffer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1006 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1008 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1009 | rv = PyBytes_FromStringAndSize(NULL, fragment->len*2); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1010 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1011 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | ncp = (signed char *)PyBytes_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1013 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1014 | for (i = 0; i < fragment->len; i += width) { |
| 1015 | double val = GETRAWSAMPLE(width, fragment->buf, i); |
| 1016 | int val1 = (int)floor(fbound(val*lfactor, minval, maxval)); |
| 1017 | int val2 = (int)floor(fbound(val*rfactor, minval, maxval)); |
| 1018 | SETRAWSAMPLE(width, ncp, i*2, val1); |
| 1019 | SETRAWSAMPLE(width, ncp, i*2 + width, val2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | } |
| 1021 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1024 | /*[clinic input] |
| 1025 | audioop.add |
| 1026 | |
| 1027 | fragment1: Py_buffer |
| 1028 | fragment2: Py_buffer |
| 1029 | width: int |
| 1030 | / |
| 1031 | |
| 1032 | Return a fragment which is the addition of the two samples passed as parameters. |
| 1033 | [clinic start generated code]*/ |
| 1034 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1035 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1036 | audioop_add_impl(PyModuleDef *module, Py_buffer *fragment1, Py_buffer *fragment2, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1037 | /*[clinic end generated code: output=f9218bf9ea75c3f1 input=4a8d4bae4c1605c7]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1038 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1039 | signed char *ncp; |
| 1040 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1041 | int minval, maxval, newval; |
| 1042 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1043 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1044 | if (!audioop_check_parameters(fragment1->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1045 | return NULL; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1046 | if (fragment1->len != fragment2->len) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | PyErr_SetString(AudioopError, "Lengths should be the same"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1048 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1050 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1051 | maxval = maxvals[width]; |
| 1052 | minval = minvals[width]; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1053 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1054 | rv = PyBytes_FromStringAndSize(NULL, fragment1->len); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1055 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1056 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | ncp = (signed char *)PyBytes_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1058 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1059 | for (i = 0; i < fragment1->len; i += width) { |
| 1060 | int val1 = GETRAWSAMPLE(width, fragment1->buf, i); |
| 1061 | int val2 = GETRAWSAMPLE(width, fragment2->buf, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1063 | if (width < 4) { |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1064 | newval = val1 + val2; |
| 1065 | /* truncate in case of overflow */ |
| 1066 | if (newval > maxval) |
| 1067 | newval = maxval; |
| 1068 | else if (newval < minval) |
| 1069 | newval = minval; |
| 1070 | } |
| 1071 | else { |
| 1072 | double fval = (double)val1 + (double)val2; |
| 1073 | /* truncate in case of overflow */ |
| 1074 | newval = (int)floor(fbound(fval, minval, maxval)); |
| 1075 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1077 | SETRAWSAMPLE(width, ncp, i, newval); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | } |
| 1079 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1082 | /*[clinic input] |
| 1083 | audioop.bias |
| 1084 | |
| 1085 | fragment: Py_buffer |
| 1086 | width: int |
| 1087 | bias: int |
| 1088 | / |
| 1089 | |
| 1090 | Return a fragment that is the original fragment with a bias added to each sample. |
| 1091 | [clinic start generated code]*/ |
| 1092 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1093 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1094 | audioop_bias_impl(PyModuleDef *module, Py_buffer *fragment, int width, int bias) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1095 | /*[clinic end generated code: output=8ec80b3f5d510a51 input=2b5cce5c3bb4838c]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1096 | { |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1097 | signed char *ncp; |
| 1098 | Py_ssize_t i; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1099 | unsigned int val = 0, mask; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1100 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1101 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1102 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1103 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1104 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1105 | rv = PyBytes_FromStringAndSize(NULL, fragment->len); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1106 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1107 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | ncp = (signed char *)PyBytes_AsString(rv); |
| 1109 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1110 | mask = masks[width]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1112 | for (i = 0; i < fragment->len; i += width) { |
| 1113 | if (width == 1) |
| 1114 | val = GETINTX(unsigned char, fragment->buf, i); |
| 1115 | else if (width == 2) |
| 1116 | val = GETINTX(unsigned short, fragment->buf, i); |
| 1117 | else if (width == 3) |
| 1118 | val = ((unsigned int)GETINT24(fragment->buf, i)) & 0xffffffu; |
Serhiy Storchaka | ce82eb2 | 2013-10-20 09:42:26 +0300 | [diff] [blame] | 1119 | else { |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1120 | assert(width == 4); |
| 1121 | val = GETINTX(PY_UINT32_T, fragment->buf, i); |
Serhiy Storchaka | ce82eb2 | 2013-10-20 09:42:26 +0300 | [diff] [blame] | 1122 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1124 | val += (unsigned int)bias; |
| 1125 | /* wrap around in case of overflow */ |
| 1126 | val &= mask; |
| 1127 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1128 | if (width == 1) |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1129 | SETINTX(unsigned char, ncp, i, val); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1130 | else if (width == 2) |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1131 | SETINTX(unsigned short, ncp, i, val); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1132 | else if (width == 3) |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1133 | SETINT24(ncp, i, (int)val); |
Serhiy Storchaka | ce82eb2 | 2013-10-20 09:42:26 +0300 | [diff] [blame] | 1134 | else { |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1135 | assert(width == 4); |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1136 | SETINTX(PY_UINT32_T, ncp, i, val); |
Serhiy Storchaka | ce82eb2 | 2013-10-20 09:42:26 +0300 | [diff] [blame] | 1137 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | } |
| 1139 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1142 | /*[clinic input] |
| 1143 | audioop.reverse |
| 1144 | |
| 1145 | fragment: Py_buffer |
| 1146 | width: int |
| 1147 | / |
| 1148 | |
| 1149 | Reverse the samples in a fragment and returns the modified fragment. |
| 1150 | [clinic start generated code]*/ |
| 1151 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1152 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1153 | audioop_reverse_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1154 | /*[clinic end generated code: output=6ec3c91337f5925e input=668f890cf9f9d225]*/ |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | unsigned char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1157 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1158 | PyObject *rv; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1159 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1160 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1161 | return NULL; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1162 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1163 | rv = PyBytes_FromStringAndSize(NULL, fragment->len); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1164 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1165 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1167 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1168 | for (i = 0; i < fragment->len; i += width) { |
| 1169 | int val = GETRAWSAMPLE(width, fragment->buf, i); |
| 1170 | SETRAWSAMPLE(width, ncp, fragment->len - i - width, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | } |
| 1172 | return rv; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1175 | /*[clinic input] |
| 1176 | audioop.byteswap |
| 1177 | |
| 1178 | fragment: Py_buffer |
| 1179 | width: int |
| 1180 | / |
| 1181 | |
| 1182 | Convert big-endian samples to little-endian and vice versa. |
| 1183 | [clinic start generated code]*/ |
| 1184 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1185 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1186 | audioop_byteswap_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1187 | /*[clinic end generated code: output=bfe4aa584b7a3f5b input=fae7611ceffa5c82]*/ |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1188 | { |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1189 | unsigned char *ncp; |
| 1190 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1191 | PyObject *rv; |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1192 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1193 | if (!audioop_check_parameters(fragment->len, width)) |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1194 | return NULL; |
| 1195 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1196 | rv = PyBytes_FromStringAndSize(NULL, fragment->len); |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1197 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1198 | return NULL; |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1199 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1200 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1201 | for (i = 0; i < fragment->len; i += width) { |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1202 | int j; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1203 | for (j = 0; j < width; j++) |
| 1204 | ncp[i + width - 1 - j] = ((unsigned char *)fragment->buf)[i + j]; |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1205 | } |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1206 | return rv; |
| 1207 | } |
| 1208 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1209 | /*[clinic input] |
| 1210 | audioop.lin2lin |
| 1211 | |
| 1212 | fragment: Py_buffer |
| 1213 | width: int |
| 1214 | newwidth: int |
| 1215 | / |
| 1216 | |
| 1217 | Convert samples between 1-, 2-, 3- and 4-byte formats. |
| 1218 | [clinic start generated code]*/ |
| 1219 | |
Serhiy Storchaka | 3062c9a | 2013-11-23 22:26:01 +0200 | [diff] [blame] | 1220 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1221 | audioop_lin2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width, int newwidth) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1222 | /*[clinic end generated code: output=3f9468a74472a93e input=5ce08c8aa2f24d96]*/ |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1224 | unsigned char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1225 | Py_ssize_t i, j; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1226 | PyObject *rv; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1227 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1228 | if (!audioop_check_parameters(fragment->len, width)) |
| 1229 | return NULL; |
| 1230 | if (!audioop_check_size(newwidth)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1231 | return NULL; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1232 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1233 | if (fragment->len/width > PY_SSIZE_T_MAX/newwidth) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | PyErr_SetString(PyExc_MemoryError, |
| 1235 | "not enough memory for output buffer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1236 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1238 | rv = PyBytes_FromStringAndSize(NULL, (fragment->len/width)*newwidth); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1239 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1240 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1242 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1243 | for (i = j = 0; i < fragment->len; i += width, j += newwidth) { |
| 1244 | int val = GETSAMPLE32(width, fragment->buf, i); |
| 1245 | SETSAMPLE32(newwidth, ncp, j, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1246 | } |
| 1247 | return rv; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1250 | static int |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1251 | gcd(int a, int b) |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1253 | while (b > 0) { |
| 1254 | int tmp = a % b; |
| 1255 | a = b; |
| 1256 | b = tmp; |
| 1257 | } |
| 1258 | return a; |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1261 | /*[clinic input] |
| 1262 | audioop.ratecv |
| 1263 | |
| 1264 | fragment: Py_buffer |
| 1265 | width: int |
| 1266 | nchannels: int |
| 1267 | inrate: int |
| 1268 | outrate: int |
| 1269 | state: object |
| 1270 | weightA: int = 1 |
| 1271 | weightB: int = 0 |
| 1272 | / |
| 1273 | |
| 1274 | Convert the frame rate of the input fragment. |
| 1275 | [clinic start generated code]*/ |
| 1276 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1277 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1278 | audioop_ratecv_impl(PyModuleDef *module, Py_buffer *fragment, int width, int nchannels, int inrate, int outrate, PyObject *state, int weightA, int weightB) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1279 | /*[clinic end generated code: output=5585dddc4b5ff236 input=aff3acdc94476191]*/ |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | char *cp, *ncp; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1282 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | int chan, d, *prev_i, *cur_i, cur_o; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1284 | PyObject *samps, *str, *rv = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | int bytes_per_frame; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1286 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1287 | if (!audioop_check_size(width)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | if (nchannels < 1) { |
| 1290 | PyErr_SetString(AudioopError, "# of channels should be >= 1"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1291 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1293 | if (width > INT_MAX / nchannels) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | /* This overflow test is rigorously correct because |
| 1295 | both multiplicands are >= 1. Use the argument names |
| 1296 | from the docs for the error msg. */ |
| 1297 | PyErr_SetString(PyExc_OverflowError, |
| 1298 | "width * nchannels too big for a C int"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1299 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1301 | bytes_per_frame = width * nchannels; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | if (weightA < 1 || weightB < 0) { |
| 1303 | PyErr_SetString(AudioopError, |
| 1304 | "weightA should be >= 1, weightB should be >= 0"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1305 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | } |
Christian Heimes | c4ab9a4 | 2014-01-27 01:12:00 +0100 | [diff] [blame] | 1307 | assert(fragment->len >= 0); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1308 | if (fragment->len % bytes_per_frame != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | PyErr_SetString(AudioopError, "not a whole number of frames"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1310 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | } |
| 1312 | if (inrate <= 0 || outrate <= 0) { |
| 1313 | PyErr_SetString(AudioopError, "sampling rate not > 0"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1314 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | } |
| 1316 | /* divide inrate and outrate by their greatest common divisor */ |
| 1317 | d = gcd(inrate, outrate); |
| 1318 | inrate /= d; |
| 1319 | outrate /= d; |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1320 | /* divide weightA and weightB by their greatest common divisor */ |
| 1321 | d = gcd(weightA, weightB); |
| 1322 | weightA /= d; |
| 1323 | weightA /= d; |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1324 | |
Mark Dickinson | 85eacea | 2010-05-10 16:27:45 +0000 | [diff] [blame] | 1325 | if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | PyErr_SetString(PyExc_MemoryError, |
| 1327 | "not enough memory for output buffer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1328 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1330 | prev_i = (int *) PyMem_Malloc(nchannels * sizeof(int)); |
| 1331 | cur_i = (int *) PyMem_Malloc(nchannels * sizeof(int)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | if (prev_i == NULL || cur_i == NULL) { |
| 1333 | (void) PyErr_NoMemory(); |
| 1334 | goto exit; |
| 1335 | } |
| 1336 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1337 | len = fragment->len / bytes_per_frame; /* # of frames */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | |
| 1339 | if (state == Py_None) { |
| 1340 | d = -outrate; |
| 1341 | for (chan = 0; chan < nchannels; chan++) |
| 1342 | prev_i[chan] = cur_i[chan] = 0; |
| 1343 | } |
| 1344 | else { |
| 1345 | if (!PyArg_ParseTuple(state, |
| 1346 | "iO!;audioop.ratecv: illegal state argument", |
| 1347 | &d, &PyTuple_Type, &samps)) |
| 1348 | goto exit; |
| 1349 | if (PyTuple_Size(samps) != nchannels) { |
| 1350 | PyErr_SetString(AudioopError, |
| 1351 | "illegal state argument"); |
| 1352 | goto exit; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 1353 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | for (chan = 0; chan < nchannels; chan++) { |
| 1355 | if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan), |
| 1356 | "ii:ratecv", &prev_i[chan], |
| 1357 | &cur_i[chan])) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1358 | goto exit; |
| 1359 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | /* str <- Space for the output buffer. */ |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1363 | if (len == 0) |
| 1364 | str = PyBytes_FromStringAndSize(NULL, 0); |
| 1365 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | /* There are len input frames, so we need (mathematically) |
| 1367 | ceiling(len*outrate/inrate) output frames, and each frame |
| 1368 | requires bytes_per_frame bytes. Computing this |
| 1369 | without spurious overflow is the challenge; we can |
Mark Dickinson | 393b97a | 2010-05-11 13:09:58 +0000 | [diff] [blame] | 1370 | settle for a reasonable upper bound, though, in this |
| 1371 | case ceiling(len/inrate) * outrate. */ |
| 1372 | |
| 1373 | /* compute ceiling(len/inrate) without overflow */ |
Christian Heimes | c4ab9a4 | 2014-01-27 01:12:00 +0100 | [diff] [blame] | 1374 | Py_ssize_t q = 1 + (len - 1) / inrate; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1375 | if (outrate > PY_SSIZE_T_MAX / q / bytes_per_frame) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | str = NULL; |
| 1377 | else |
Mark Dickinson | 393b97a | 2010-05-11 13:09:58 +0000 | [diff] [blame] | 1378 | str = PyBytes_FromStringAndSize(NULL, |
| 1379 | q * outrate * bytes_per_frame); |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1380 | } |
| 1381 | if (str == NULL) { |
| 1382 | PyErr_SetString(PyExc_MemoryError, |
| 1383 | "not enough memory for output buffer"); |
| 1384 | goto exit; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | } |
| 1386 | ncp = PyBytes_AsString(str); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1387 | cp = fragment->buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | |
| 1389 | for (;;) { |
| 1390 | while (d < 0) { |
| 1391 | if (len == 0) { |
| 1392 | samps = PyTuple_New(nchannels); |
| 1393 | if (samps == NULL) |
| 1394 | goto exit; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1395 | for (chan = 0; chan < nchannels; chan++) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | PyTuple_SetItem(samps, chan, |
| 1397 | Py_BuildValue("(ii)", |
| 1398 | prev_i[chan], |
| 1399 | cur_i[chan])); |
| 1400 | if (PyErr_Occurred()) |
| 1401 | goto exit; |
| 1402 | /* We have checked before that the length |
| 1403 | * of the string fits into int. */ |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1404 | len = (Py_ssize_t)(ncp - PyBytes_AsString(str)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | rv = PyBytes_FromStringAndSize |
| 1406 | (PyBytes_AsString(str), len); |
| 1407 | Py_DECREF(str); |
| 1408 | str = rv; |
| 1409 | if (str == NULL) |
| 1410 | goto exit; |
| 1411 | rv = Py_BuildValue("(O(iO))", str, d, samps); |
| 1412 | Py_DECREF(samps); |
| 1413 | Py_DECREF(str); |
| 1414 | goto exit; /* return rv */ |
| 1415 | } |
| 1416 | for (chan = 0; chan < nchannels; chan++) { |
| 1417 | prev_i[chan] = cur_i[chan]; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1418 | cur_i[chan] = GETSAMPLE32(width, cp, 0); |
| 1419 | cp += width; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | /* implements a simple digital filter */ |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1421 | cur_i[chan] = (int)( |
| 1422 | ((double)weightA * (double)cur_i[chan] + |
| 1423 | (double)weightB * (double)prev_i[chan]) / |
| 1424 | ((double)weightA + (double)weightB)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | } |
| 1426 | len--; |
| 1427 | d += outrate; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1428 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | while (d >= 0) { |
| 1430 | for (chan = 0; chan < nchannels; chan++) { |
Serhiy Storchaka | 01ad622 | 2013-02-09 11:10:53 +0200 | [diff] [blame] | 1431 | cur_o = (int)(((double)prev_i[chan] * (double)d + |
| 1432 | (double)cur_i[chan] * (double)(outrate - d)) / |
| 1433 | (double)outrate); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1434 | SETSAMPLE32(width, ncp, 0, cur_o); |
| 1435 | ncp += width; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | } |
| 1437 | d -= inrate; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1438 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | } |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame] | 1440 | exit: |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1441 | PyMem_Free(prev_i); |
| 1442 | PyMem_Free(cur_i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | return rv; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1444 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1445 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1446 | /*[clinic input] |
| 1447 | audioop.lin2ulaw |
| 1448 | |
| 1449 | fragment: Py_buffer |
| 1450 | width: int |
| 1451 | / |
| 1452 | |
| 1453 | Convert samples in the audio fragment to u-LAW encoding. |
| 1454 | [clinic start generated code]*/ |
| 1455 | |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1456 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1457 | audioop_lin2ulaw_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1458 | /*[clinic end generated code: output=26263cc877c5e1bc input=2450d1b870b6bac2]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | unsigned char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1461 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1462 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1463 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1464 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1465 | return NULL; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1466 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1467 | rv = PyBytes_FromStringAndSize(NULL, fragment->len/width); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1468 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1469 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1471 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1472 | for (i = 0; i < fragment->len; i += width) { |
| 1473 | int val = GETSAMPLE32(width, fragment->buf, i); |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1474 | *ncp++ = st_14linear2ulaw(val >> 18); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | } |
| 1476 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1479 | /*[clinic input] |
| 1480 | audioop.ulaw2lin |
| 1481 | |
| 1482 | fragment: Py_buffer |
| 1483 | width: int |
| 1484 | / |
| 1485 | |
| 1486 | Convert sound fragments in u-LAW encoding to linearly encoded sound fragments. |
| 1487 | [clinic start generated code]*/ |
| 1488 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1489 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1490 | audioop_ulaw2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1491 | /*[clinic end generated code: output=9864cb34e3a1d876 input=45d53ddce5be7d06]*/ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1492 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | unsigned char *cp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | signed char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1495 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1496 | PyObject *rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1497 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1498 | if (!audioop_check_size(width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1499 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1501 | if (fragment->len > PY_SSIZE_T_MAX/width) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | PyErr_SetString(PyExc_MemoryError, |
| 1503 | "not enough memory for output buffer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1504 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1505 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1506 | rv = PyBytes_FromStringAndSize(NULL, fragment->len*width); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1507 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1508 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | ncp = (signed char *)PyBytes_AsString(rv); |
| 1510 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1511 | cp = fragment->buf; |
| 1512 | for (i = 0; i < fragment->len*width; i += width) { |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1513 | int val = st_ulaw2linear16(*cp++) << 16; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1514 | SETSAMPLE32(width, ncp, i, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | } |
| 1516 | return rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1519 | /*[clinic input] |
| 1520 | audioop.lin2alaw |
| 1521 | |
| 1522 | fragment: Py_buffer |
| 1523 | width: int |
| 1524 | / |
| 1525 | |
| 1526 | Convert samples in the audio fragment to a-LAW encoding. |
| 1527 | [clinic start generated code]*/ |
| 1528 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1529 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1530 | audioop_lin2alaw_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1531 | /*[clinic end generated code: output=d5bf14bd0fe6fdcd input=ffb1ef8bb39da945]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | unsigned char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1534 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1535 | PyObject *rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1536 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1537 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1538 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1539 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1540 | rv = PyBytes_FromStringAndSize(NULL, fragment->len/width); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1541 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1542 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | ncp = (unsigned char *)PyBytes_AsString(rv); |
| 1544 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1545 | for (i = 0; i < fragment->len; i += width) { |
| 1546 | int val = GETSAMPLE32(width, fragment->buf, i); |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1547 | *ncp++ = st_linear2alaw(val >> 19); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | } |
| 1549 | return rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1552 | /*[clinic input] |
| 1553 | audioop.alaw2lin |
| 1554 | |
| 1555 | fragment: Py_buffer |
| 1556 | width: int |
| 1557 | / |
| 1558 | |
| 1559 | Convert sound fragments in a-LAW encoding to linearly encoded sound fragments. |
| 1560 | [clinic start generated code]*/ |
| 1561 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1562 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1563 | audioop_alaw2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1564 | /*[clinic end generated code: output=d2b604ddd036e1cd input=4140626046cd1772]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | unsigned char *cp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | signed char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1568 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1569 | int val; |
| 1570 | PyObject *rv; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1571 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1572 | if (!audioop_check_size(width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1573 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1575 | if (fragment->len > PY_SSIZE_T_MAX/width) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | PyErr_SetString(PyExc_MemoryError, |
| 1577 | "not enough memory for output buffer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1578 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1580 | rv = PyBytes_FromStringAndSize(NULL, fragment->len*width); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1581 | if (rv == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1582 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | ncp = (signed char *)PyBytes_AsString(rv); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1584 | cp = fragment->buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1586 | for (i = 0; i < fragment->len*width; i += width) { |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1587 | val = st_alaw2linear16(*cp++) << 16; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1588 | SETSAMPLE32(width, ncp, i, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | } |
| 1590 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1593 | /*[clinic input] |
| 1594 | audioop.lin2adpcm |
| 1595 | |
| 1596 | fragment: Py_buffer |
| 1597 | width: int |
| 1598 | state: object |
| 1599 | / |
| 1600 | |
| 1601 | Convert samples to 4 bit Intel/DVI ADPCM encoding. |
| 1602 | [clinic start generated code]*/ |
| 1603 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1604 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1605 | audioop_lin2adpcm_impl(PyModuleDef *module, Py_buffer *fragment, int width, PyObject *state) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1606 | /*[clinic end generated code: output=4654c29d2731fafe input=12919d549b90c90a]*/ |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1607 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1608 | signed char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1609 | Py_ssize_t i; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1610 | int step, valpred, delta, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | index, sign, vpdiff, diff; |
Benjamin Peterson | 08673c5 | 2014-01-26 10:24:24 -0500 | [diff] [blame] | 1612 | PyObject *rv = NULL, *str; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1613 | int outputbuffer = 0, bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1614 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1615 | if (!audioop_check_parameters(fragment->len, width)) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 1616 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1618 | str = PyBytes_FromStringAndSize(NULL, fragment->len/(width*2)); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1619 | if (str == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1620 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | ncp = (signed char *)PyBytes_AsString(str); |
| 1622 | |
| 1623 | /* Decode state, should have (value, step) */ |
| 1624 | if ( state == Py_None ) { |
| 1625 | /* First time, it seems. Set defaults */ |
| 1626 | valpred = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | index = 0; |
Victor Stinner | daeffd2 | 2014-01-03 03:26:47 +0100 | [diff] [blame] | 1628 | } else if (!PyTuple_Check(state)) { |
| 1629 | PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); |
Benjamin Peterson | 08673c5 | 2014-01-26 10:24:24 -0500 | [diff] [blame] | 1630 | goto exit; |
| 1631 | } else if (!PyArg_ParseTuple(state, "ii", &valpred, &index)) { |
| 1632 | goto exit; |
| 1633 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1634 | |
| 1635 | step = stepsizeTable[index]; |
| 1636 | bufferstep = 1; |
| 1637 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1638 | for (i = 0; i < fragment->len; i += width) { |
| 1639 | int val = GETSAMPLE32(width, fragment->buf, i) >> 16; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* Step 1 - compute difference with previous value */ |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1642 | if (val < valpred) { |
| 1643 | diff = valpred - val; |
| 1644 | sign = 8; |
| 1645 | } |
| 1646 | else { |
| 1647 | diff = val - valpred; |
| 1648 | sign = 0; |
| 1649 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | |
| 1651 | /* Step 2 - Divide and clamp */ |
| 1652 | /* Note: |
| 1653 | ** This code *approximately* computes: |
| 1654 | ** delta = diff*4/step; |
| 1655 | ** vpdiff = (delta+0.5)*step/4; |
| 1656 | ** but in shift step bits are dropped. The net result of this |
| 1657 | ** is that even if you have fast mul/div hardware you cannot |
| 1658 | ** put it to good use since the fixup would be too expensive. |
| 1659 | */ |
| 1660 | delta = 0; |
| 1661 | vpdiff = (step >> 3); |
| 1662 | |
| 1663 | if ( diff >= step ) { |
| 1664 | delta = 4; |
| 1665 | diff -= step; |
| 1666 | vpdiff += step; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1667 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | step >>= 1; |
| 1669 | if ( diff >= step ) { |
| 1670 | delta |= 2; |
| 1671 | diff -= step; |
| 1672 | vpdiff += step; |
| 1673 | } |
| 1674 | step >>= 1; |
| 1675 | if ( diff >= step ) { |
| 1676 | delta |= 1; |
| 1677 | vpdiff += step; |
| 1678 | } |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | /* Step 3 - Update previous value */ |
| 1681 | if ( sign ) |
| 1682 | valpred -= vpdiff; |
| 1683 | else |
| 1684 | valpred += vpdiff; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | /* Step 4 - Clamp previous value to 16 bits */ |
| 1687 | if ( valpred > 32767 ) |
| 1688 | valpred = 32767; |
| 1689 | else if ( valpred < -32768 ) |
| 1690 | valpred = -32768; |
| 1691 | |
| 1692 | /* Step 5 - Assemble value, update index and step values */ |
| 1693 | delta |= sign; |
| 1694 | |
| 1695 | index += indexTable[delta]; |
| 1696 | if ( index < 0 ) index = 0; |
| 1697 | if ( index > 88 ) index = 88; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1698 | step = stepsizeTable[index]; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | /* Step 6 - Output value */ |
| 1701 | if ( bufferstep ) { |
| 1702 | outputbuffer = (delta << 4) & 0xf0; |
| 1703 | } else { |
| 1704 | *ncp++ = (delta & 0x0f) | outputbuffer; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1705 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | bufferstep = !bufferstep; |
| 1707 | } |
| 1708 | rv = Py_BuildValue("(O(ii))", str, valpred, index); |
Benjamin Peterson | 08673c5 | 2014-01-26 10:24:24 -0500 | [diff] [blame] | 1709 | |
| 1710 | exit: |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1711 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | return rv; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1715 | /*[clinic input] |
| 1716 | audioop.adpcm2lin |
| 1717 | |
| 1718 | fragment: Py_buffer |
| 1719 | width: int |
| 1720 | state: object |
| 1721 | / |
| 1722 | |
| 1723 | Decode an Intel/DVI ADPCM coded fragment to a linear fragment. |
| 1724 | [clinic start generated code]*/ |
| 1725 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1726 | static PyObject * |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1727 | audioop_adpcm2lin_impl(PyModuleDef *module, Py_buffer *fragment, int width, PyObject *state) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1728 | /*[clinic end generated code: output=371965cdcc0aa69b input=f5221144f5ca9ef0]*/ |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | signed char *cp; |
| 1731 | signed char *ncp; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1732 | Py_ssize_t i, outlen; |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1733 | int valpred, step, delta, index, sign, vpdiff; |
| 1734 | PyObject *rv, *str; |
Mark Dickinson | 81fece2 | 2010-05-11 13:34:35 +0000 | [diff] [blame] | 1735 | int inputbuffer = 0, bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1736 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1737 | if (!audioop_check_size(width)) |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1738 | return NULL; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | /* Decode state, should have (value, step) */ |
| 1741 | if ( state == Py_None ) { |
| 1742 | /* First time, it seems. Set defaults */ |
| 1743 | valpred = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | index = 0; |
Victor Stinner | daeffd2 | 2014-01-03 03:26:47 +0100 | [diff] [blame] | 1745 | } else if (!PyTuple_Check(state)) { |
| 1746 | PyErr_SetString(PyExc_TypeError, "state must be a tuple or None"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1747 | return NULL; |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1748 | } else if (!PyArg_ParseTuple(state, "ii", &valpred, &index)) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1749 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1751 | if (fragment->len > (PY_SSIZE_T_MAX/2)/width) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | PyErr_SetString(PyExc_MemoryError, |
| 1753 | "not enough memory for output buffer"); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1754 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | } |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1756 | outlen = fragment->len*width*2; |
Serhiy Storchaka | eaea5e9 | 2013-10-19 21:10:46 +0300 | [diff] [blame] | 1757 | str = PyBytes_FromStringAndSize(NULL, outlen); |
Serhiy Storchaka | 711e91b | 2013-11-10 21:44:36 +0200 | [diff] [blame] | 1758 | if (str == NULL) |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1759 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | ncp = (signed char *)PyBytes_AsString(str); |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1761 | cp = fragment->buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | |
| 1763 | step = stepsizeTable[index]; |
| 1764 | bufferstep = 0; |
| 1765 | |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1766 | for (i = 0; i < outlen; i += width) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1767 | /* Step 1 - get the delta value and compute next index */ |
| 1768 | if ( bufferstep ) { |
| 1769 | delta = inputbuffer & 0xf; |
| 1770 | } else { |
| 1771 | inputbuffer = *cp++; |
| 1772 | delta = (inputbuffer >> 4) & 0xf; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1773 | } |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | bufferstep = !bufferstep; |
| 1776 | |
| 1777 | /* Step 2 - Find new index value (for later) */ |
| 1778 | index += indexTable[delta]; |
| 1779 | if ( index < 0 ) index = 0; |
| 1780 | if ( index > 88 ) index = 88; |
| 1781 | |
| 1782 | /* Step 3 - Separate sign and magnitude */ |
| 1783 | sign = delta & 8; |
| 1784 | delta = delta & 7; |
| 1785 | |
| 1786 | /* Step 4 - Compute difference and new predicted value */ |
| 1787 | /* |
| 1788 | ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment |
| 1789 | ** in adpcm_coder. |
| 1790 | */ |
| 1791 | vpdiff = step >> 3; |
| 1792 | if ( delta & 4 ) vpdiff += step; |
| 1793 | if ( delta & 2 ) vpdiff += step>>1; |
| 1794 | if ( delta & 1 ) vpdiff += step>>2; |
| 1795 | |
| 1796 | if ( sign ) |
| 1797 | valpred -= vpdiff; |
| 1798 | else |
| 1799 | valpred += vpdiff; |
| 1800 | |
| 1801 | /* Step 5 - clamp output value */ |
| 1802 | if ( valpred > 32767 ) |
| 1803 | valpred = 32767; |
| 1804 | else if ( valpred < -32768 ) |
| 1805 | valpred = -32768; |
| 1806 | |
| 1807 | /* Step 6 - Update step value */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1808 | step = stepsizeTable[index]; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1810 | /* Step 6 - Output value */ |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1811 | SETSAMPLE32(width, ncp, i, valpred << 16); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | rv = Py_BuildValue("(O(ii))", str, valpred, index); |
| 1815 | Py_DECREF(str); |
| 1816 | return rv; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 1819 | #include "clinic/audioop.c.h" |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1820 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1821 | static PyMethodDef audioop_methods[] = { |
Serhiy Storchaka | 8d00d73 | 2014-01-25 11:57:59 +0200 | [diff] [blame] | 1822 | AUDIOOP_MAX_METHODDEF |
| 1823 | AUDIOOP_MINMAX_METHODDEF |
| 1824 | AUDIOOP_AVG_METHODDEF |
| 1825 | AUDIOOP_MAXPP_METHODDEF |
| 1826 | AUDIOOP_AVGPP_METHODDEF |
| 1827 | AUDIOOP_RMS_METHODDEF |
| 1828 | AUDIOOP_FINDFIT_METHODDEF |
| 1829 | AUDIOOP_FINDMAX_METHODDEF |
| 1830 | AUDIOOP_FINDFACTOR_METHODDEF |
| 1831 | AUDIOOP_CROSS_METHODDEF |
| 1832 | AUDIOOP_MUL_METHODDEF |
| 1833 | AUDIOOP_ADD_METHODDEF |
| 1834 | AUDIOOP_BIAS_METHODDEF |
| 1835 | AUDIOOP_ULAW2LIN_METHODDEF |
| 1836 | AUDIOOP_LIN2ULAW_METHODDEF |
| 1837 | AUDIOOP_ALAW2LIN_METHODDEF |
| 1838 | AUDIOOP_LIN2ALAW_METHODDEF |
| 1839 | AUDIOOP_LIN2LIN_METHODDEF |
| 1840 | AUDIOOP_ADPCM2LIN_METHODDEF |
| 1841 | AUDIOOP_LIN2ADPCM_METHODDEF |
| 1842 | AUDIOOP_TOMONO_METHODDEF |
| 1843 | AUDIOOP_TOSTEREO_METHODDEF |
| 1844 | AUDIOOP_GETSAMPLE_METHODDEF |
| 1845 | AUDIOOP_REVERSE_METHODDEF |
| 1846 | AUDIOOP_BYTESWAP_METHODDEF |
| 1847 | AUDIOOP_RATECV_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | { 0, 0 } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1849 | }; |
| 1850 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1851 | |
| 1852 | static struct PyModuleDef audioopmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | PyModuleDef_HEAD_INIT, |
| 1854 | "audioop", |
| 1855 | NULL, |
| 1856 | -1, |
| 1857 | audioop_methods, |
| 1858 | NULL, |
| 1859 | NULL, |
| 1860 | NULL, |
| 1861 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1862 | }; |
| 1863 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 1864 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1865 | PyInit_audioop(void) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1866 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | PyObject *m, *d; |
| 1868 | m = PyModule_Create(&audioopmodule); |
| 1869 | if (m == NULL) |
| 1870 | return NULL; |
| 1871 | d = PyModule_GetDict(m); |
| 1872 | if (d == NULL) |
| 1873 | return NULL; |
| 1874 | AudioopError = PyErr_NewException("audioop.error", NULL, NULL); |
| 1875 | if (AudioopError != NULL) |
| 1876 | PyDict_SetItemString(d,"error",AudioopError); |
| 1877 | return m; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1878 | } |