Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 32 | /* audioopmodule - Module to detect peak values in arrays */ |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 33 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 36 | #if SIZEOF_INT == 4 |
| 37 | typedef int Py_Int32; |
| 38 | typedef unsigned int Py_UInt32; |
| 39 | #else |
| 40 | #if SIZEOF_LONG == 4 |
| 41 | typedef long Py_Int32; |
| 42 | typedef unsigned long Py_UInt32; |
| 43 | #else |
| 44 | #error "No 4-byte integral type" |
| 45 | #endif |
| 46 | #endif |
| 47 | |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 48 | #if defined(__CHAR_UNSIGNED__) |
| 49 | #if defined(signed) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 50 | !ERROR!; READ THE SOURCE FILE!; |
| 51 | /* This module currently does not work on systems where only unsigned |
| 52 | characters are available. Take it out of Setup. Sorry. */ |
| 53 | #endif |
Guido van Rossum | 7b1e974 | 1994-08-29 10:46:42 +0000 | [diff] [blame] | 54 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | a320fd3 | 1995-03-09 12:14:15 +0000 | [diff] [blame] | 56 | #include "mymath.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 57 | |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 58 | /* Code shamelessly stolen from sox, |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 59 | ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */ |
| 60 | |
| 61 | #define MINLIN -32768 |
| 62 | #define MAXLIN 32767 |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 63 | #define LINCLIP(x) do { if ( x < MINLIN ) x = MINLIN ; \ |
| 64 | else if ( x > MAXLIN ) x = MAXLIN; \ |
| 65 | } while ( 0 ) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | cd938fc | 1995-01-17 16:13:48 +0000 | [diff] [blame] | 67 | static unsigned char st_linear_to_ulaw( /* int sample */ ); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | ** This macro converts from ulaw to 16 bit linear, faster. |
| 71 | ** |
| 72 | ** Jef Poskanzer |
| 73 | ** 23 October 1989 |
| 74 | ** |
| 75 | ** Input: 8 bit ulaw sample |
| 76 | ** Output: signed 16 bit linear sample |
| 77 | */ |
| 78 | #define st_ulaw_to_linear(ulawbyte) ulaw_table[ulawbyte] |
| 79 | |
| 80 | static int ulaw_table[256] = { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 81 | -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, |
| 82 | -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, |
| 83 | -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, |
| 84 | -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, |
| 85 | -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, |
| 86 | -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, |
| 87 | -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, |
| 88 | -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, |
| 89 | -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, |
| 90 | -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, |
| 91 | -876, -844, -812, -780, -748, -716, -684, -652, |
| 92 | -620, -588, -556, -524, -492, -460, -428, -396, |
| 93 | -372, -356, -340, -324, -308, -292, -276, -260, |
| 94 | -244, -228, -212, -196, -180, -164, -148, -132, |
| 95 | -120, -112, -104, -96, -88, -80, -72, -64, |
| 96 | -56, -48, -40, -32, -24, -16, -8, 0, |
| 97 | 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, |
| 98 | 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, |
| 99 | 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, |
| 100 | 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, |
| 101 | 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, |
| 102 | 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, |
| 103 | 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, |
| 104 | 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, |
| 105 | 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, |
| 106 | 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, |
| 107 | 876, 844, 812, 780, 748, 716, 684, 652, |
| 108 | 620, 588, 556, 524, 492, 460, 428, 396, |
| 109 | 372, 356, 340, 324, 308, 292, 276, 260, |
| 110 | 244, 228, 212, 196, 180, 164, 148, 132, |
| 111 | 120, 112, 104, 96, 88, 80, 72, 64, |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 112 | 56, 48, 40, 32, 24, 16, 8, 0 }; |
| 113 | |
| 114 | #define ZEROTRAP /* turn on the trap as per the MIL-STD */ |
| 115 | #define BIAS 0x84 /* define the add-in bias for 16 bit samples */ |
| 116 | #define CLIP 32635 |
| 117 | |
Guido van Rossum | cd938fc | 1995-01-17 16:13:48 +0000 | [diff] [blame] | 118 | static unsigned char |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 119 | st_linear_to_ulaw( sample ) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 120 | int sample; |
| 121 | { |
| 122 | static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3, |
| 123 | 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, |
| 124 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 125 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 126 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 127 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 128 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 129 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 130 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 131 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 132 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 133 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 134 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 135 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 136 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 137 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}; |
| 138 | int sign, exponent, mantissa; |
| 139 | unsigned char ulawbyte; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 140 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 141 | /* Get the sample into sign-magnitude. */ |
| 142 | sign = (sample >> 8) & 0x80; /* set aside the sign */ |
| 143 | if ( sign != 0 ) sample = -sample; /* get magnitude */ |
| 144 | if ( sample > CLIP ) sample = CLIP; /* clip the magnitude */ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 145 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 146 | /* Convert from 16 bit linear to ulaw. */ |
| 147 | sample = sample + BIAS; |
| 148 | exponent = exp_lut[( sample >> 7 ) & 0xFF]; |
| 149 | mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F; |
| 150 | ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa ); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 151 | #ifdef ZEROTRAP |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 152 | if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */ |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 153 | #endif |
| 154 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 155 | return ulawbyte; |
| 156 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 157 | /* End of code taken from sox */ |
| 158 | |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 159 | /* Intel ADPCM step variation table */ |
| 160 | static int indexTable[16] = { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 161 | -1, -1, -1, -1, 2, 4, 6, 8, |
| 162 | -1, -1, -1, -1, 2, 4, 6, 8, |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | static int stepsizeTable[89] = { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 166 | 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
| 167 | 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, |
| 168 | 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, |
| 169 | 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, |
| 170 | 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, |
| 171 | 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, |
| 172 | 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, |
| 173 | 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, |
| 174 | 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 177 | #define CHARP(cp, i) ((signed char *)(cp+i)) |
| 178 | #define SHORTP(cp, i) ((short *)(cp+i)) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 179 | #define LONGP(cp, i) ((Py_Int32 *)(cp+i)) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 183 | static PyObject *AudioopError; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 184 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 185 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 186 | audioop_getsample(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 187 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 188 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 189 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 190 | signed char *cp; |
| 191 | int len, size, val = 0; |
| 192 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 193 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 194 | if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &size, &i) ) |
| 195 | return 0; |
| 196 | if ( size != 1 && size != 2 && size != 4 ) { |
| 197 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 198 | return 0; |
| 199 | } |
| 200 | if ( i < 0 || i >= len/size ) { |
| 201 | PyErr_SetString(AudioopError, "Index out of range"); |
| 202 | return 0; |
| 203 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 204 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 205 | else if ( size == 2 ) val = (int)*SHORTP(cp, i*2); |
| 206 | else if ( size == 4 ) val = (int)*LONGP(cp, i*4); |
| 207 | return PyInt_FromLong(val); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 210 | static PyObject * |
| 211 | audioop_max(self, args) |
| 212 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 213 | PyObject *args; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 214 | { |
| 215 | signed char *cp; |
| 216 | int len, size, val = 0; |
| 217 | int i; |
| 218 | int max = 0; |
| 219 | |
| 220 | if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) ) |
| 221 | return 0; |
| 222 | if ( size != 1 && size != 2 && size != 4 ) { |
| 223 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 224 | return 0; |
| 225 | } |
| 226 | for ( i=0; i<len; i+= size) { |
| 227 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 228 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 229 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 230 | if ( val < 0 ) val = (-val); |
| 231 | if ( val > max ) max = val; |
| 232 | } |
| 233 | return PyInt_FromLong(max); |
| 234 | } |
| 235 | |
| 236 | static PyObject * |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 237 | audioop_minmax(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 238 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 239 | PyObject *args; |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 240 | { |
| 241 | signed char *cp; |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 242 | int len, size, val = 0; |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 243 | int i; |
| 244 | int min = 0x7fffffff, max = -0x7fffffff; |
| 245 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 246 | if (!PyArg_Parse(args, "(s#i)", &cp, &len, &size)) |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 247 | return NULL; |
| 248 | if (size != 1 && size != 2 && size != 4) { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 249 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
| 252 | for (i = 0; i < len; i += size) { |
| 253 | if (size == 1) val = (int) *CHARP(cp, i); |
| 254 | else if (size == 2) val = (int) *SHORTP(cp, i); |
| 255 | else if (size == 4) val = (int) *LONGP(cp, i); |
| 256 | if (val > max) max = val; |
| 257 | if (val < min) min = val; |
| 258 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 259 | return Py_BuildValue("(ii)", min, max); |
Sjoerd Mullender | a142613 | 1994-09-06 16:19:33 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 262 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 263 | audioop_avg(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 264 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 265 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 266 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 267 | signed char *cp; |
| 268 | int len, size, val = 0; |
| 269 | int i; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 270 | double avg = 0.0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 271 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 272 | if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) ) |
| 273 | return 0; |
| 274 | if ( size != 1 && size != 2 && size != 4 ) { |
| 275 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 276 | return 0; |
| 277 | } |
| 278 | for ( i=0; i<len; i+= size) { |
| 279 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 280 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 281 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 282 | avg += val; |
| 283 | } |
| 284 | if ( len == 0 ) |
| 285 | val = 0; |
| 286 | else |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 287 | val = (int)(avg / (double)(len/size)); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 288 | return PyInt_FromLong(val); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 291 | static PyObject * |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 292 | audioop_rms(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 293 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 294 | PyObject *args; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 295 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 296 | signed char *cp; |
| 297 | int len, size, val = 0; |
| 298 | int i; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 299 | double sum_squares = 0.0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 300 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 301 | if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) ) |
| 302 | return 0; |
| 303 | if ( size != 1 && size != 2 && size != 4 ) { |
| 304 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 305 | return 0; |
| 306 | } |
| 307 | for ( i=0; i<len; i+= size) { |
| 308 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 309 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 310 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 311 | sum_squares += (double)val*(double)val; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 312 | } |
| 313 | if ( len == 0 ) |
| 314 | val = 0; |
| 315 | else |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 316 | val = (int)sqrt(sum_squares / (double)(len/size)); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 317 | return PyInt_FromLong(val); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Guido van Rossum | cd938fc | 1995-01-17 16:13:48 +0000 | [diff] [blame] | 320 | static double _sum2(a, b, len) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 321 | short *a; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 322 | short *b; |
| 323 | int len; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 324 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 325 | int i; |
| 326 | double sum = 0.0; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 327 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 328 | for( i=0; i<len; i++) { |
| 329 | sum = sum + (double)a[i]*(double)b[i]; |
| 330 | } |
| 331 | return sum; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* |
| 335 | ** Findfit tries to locate a sample within another sample. Its main use |
| 336 | ** is in echo-cancellation (to find the feedback of the output signal in |
| 337 | ** the input signal). |
| 338 | ** The method used is as follows: |
| 339 | ** |
| 340 | ** let R be the reference signal (length n) and A the input signal (length N) |
| 341 | ** with N > n, and let all sums be over i from 0 to n-1. |
| 342 | ** |
| 343 | ** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A |
| 344 | ** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This |
| 345 | ** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2). |
| 346 | ** |
| 347 | ** Next, we compute the relative distance between the original signal and |
| 348 | ** the modified signal and minimize that over j: |
| 349 | ** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 ) => |
| 350 | ** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 ) |
| 351 | ** |
| 352 | ** In the code variables correspond as follows: |
| 353 | ** cp1 A |
| 354 | ** cp2 R |
| 355 | ** len1 N |
| 356 | ** len2 n |
| 357 | ** aj_m1 A[j-1] |
| 358 | ** aj_lm1 A[j+n-1] |
| 359 | ** sum_ri_2 sum(R[i]^2) |
| 360 | ** sum_aij_2 sum(A[i+j]^2) |
| 361 | ** sum_aij_ri sum(A[i+j]R[i]) |
| 362 | ** |
| 363 | ** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri |
| 364 | ** is completely recalculated each step. |
| 365 | */ |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 366 | static PyObject * |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 367 | audioop_findfit(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 368 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 369 | PyObject *args; |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 370 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 371 | short *cp1, *cp2; |
| 372 | int len1, len2; |
| 373 | int j, best_j; |
| 374 | double aj_m1, aj_lm1; |
| 375 | 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] | 376 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 377 | if ( !PyArg_Parse(args, "(s#s#)", &cp1, &len1, &cp2, &len2) ) |
| 378 | return 0; |
| 379 | if ( len1 & 1 || len2 & 1 ) { |
| 380 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
| 381 | return 0; |
| 382 | } |
| 383 | len1 >>= 1; |
| 384 | len2 >>= 1; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 385 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 386 | if ( len1 < len2 ) { |
| 387 | PyErr_SetString(AudioopError, "First sample should be longer"); |
| 388 | return 0; |
| 389 | } |
| 390 | sum_ri_2 = _sum2(cp2, cp2, len2); |
| 391 | sum_aij_2 = _sum2(cp1, cp1, len2); |
| 392 | sum_aij_ri = _sum2(cp1, cp2, len2); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 393 | |
| 394 | result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2; |
| 395 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 396 | best_result = result; |
| 397 | best_j = 0; |
| 398 | j = 0; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 399 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 400 | for ( j=1; j<=len1-len2; j++) { |
| 401 | aj_m1 = (double)cp1[j-1]; |
| 402 | aj_lm1 = (double)cp1[j+len2-1]; |
| 403 | |
| 404 | sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1; |
| 405 | sum_aij_ri = _sum2(cp1+j, cp2, len2); |
| 406 | |
| 407 | result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) |
| 408 | / sum_aij_2; |
| 409 | |
| 410 | if ( result < best_result ) { |
| 411 | best_result = result; |
| 412 | best_j = j; |
| 413 | } |
| 414 | |
| 415 | } |
| 416 | |
| 417 | factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 418 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 419 | return Py_BuildValue("(if)", best_j, factor); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* |
| 423 | ** findfactor finds a factor f so that the energy in A-fB is minimal. |
| 424 | ** See the comment for findfit for details. |
| 425 | */ |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 426 | static PyObject * |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 427 | audioop_findfactor(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 428 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 429 | PyObject *args; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 430 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 431 | short *cp1, *cp2; |
| 432 | int len1, len2; |
| 433 | double sum_ri_2, sum_aij_ri, result; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 434 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 435 | if ( !PyArg_Parse(args, "(s#s#)", &cp1, &len1, &cp2, &len2) ) |
| 436 | return 0; |
| 437 | if ( len1 & 1 || len2 & 1 ) { |
| 438 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
| 439 | return 0; |
| 440 | } |
| 441 | if ( len1 != len2 ) { |
| 442 | PyErr_SetString(AudioopError, "Samples should be same size"); |
| 443 | return 0; |
| 444 | } |
| 445 | len2 >>= 1; |
| 446 | sum_ri_2 = _sum2(cp2, cp2, len2); |
| 447 | sum_aij_ri = _sum2(cp1, cp2, len2); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 448 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 449 | result = sum_aij_ri / sum_ri_2; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 450 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 451 | return PyFloat_FromDouble(result); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | ** findmax returns the index of the n-sized segment of the input sample |
| 456 | ** that contains the most energy. |
| 457 | */ |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 458 | static PyObject * |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 459 | audioop_findmax(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 460 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 461 | PyObject *args; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 462 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 463 | short *cp1; |
| 464 | int len1, len2; |
| 465 | int j, best_j; |
| 466 | double aj_m1, aj_lm1; |
| 467 | double result, best_result; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 468 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 469 | if ( !PyArg_Parse(args, "(s#i)", &cp1, &len1, &len2) ) |
| 470 | return 0; |
| 471 | if ( len1 & 1 ) { |
| 472 | PyErr_SetString(AudioopError, "Strings should be even-sized"); |
| 473 | return 0; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 474 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 475 | len1 >>= 1; |
| 476 | |
| 477 | if ( len1 < len2 ) { |
| 478 | PyErr_SetString(AudioopError, "Input sample should be longer"); |
| 479 | return 0; |
| 480 | } |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 481 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 482 | result = _sum2(cp1, cp1, len2); |
| 483 | |
| 484 | best_result = result; |
| 485 | best_j = 0; |
| 486 | j = 0; |
| 487 | |
| 488 | for ( j=1; j<=len1-len2; j++) { |
| 489 | aj_m1 = (double)cp1[j-1]; |
| 490 | aj_lm1 = (double)cp1[j+len2-1]; |
| 491 | |
| 492 | result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1; |
| 493 | |
| 494 | if ( result > best_result ) { |
| 495 | best_result = result; |
| 496 | best_j = j; |
| 497 | } |
| 498 | |
| 499 | } |
| 500 | |
| 501 | return PyInt_FromLong(best_j); |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 504 | static PyObject * |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 505 | audioop_avgpp(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 506 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 507 | PyObject *args; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 508 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 509 | signed char *cp; |
| 510 | int len, size, val = 0, prevval = 0, prevextremevalid = 0, |
| 511 | prevextreme = 0; |
| 512 | int i; |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 513 | double avg = 0.0; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 514 | int diff, prevdiff, extremediff, nextreme = 0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 515 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 516 | if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) ) |
| 517 | return 0; |
| 518 | if ( size != 1 && size != 2 && size != 4 ) { |
| 519 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 520 | return 0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 521 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 522 | /* Compute first delta value ahead. Also automatically makes us |
| 523 | ** skip the first extreme value |
| 524 | */ |
| 525 | if ( size == 1 ) prevval = (int)*CHARP(cp, 0); |
| 526 | else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0); |
| 527 | else if ( size == 4 ) prevval = (int)*LONGP(cp, 0); |
| 528 | if ( size == 1 ) val = (int)*CHARP(cp, size); |
| 529 | else if ( size == 2 ) val = (int)*SHORTP(cp, size); |
| 530 | else if ( size == 4 ) val = (int)*LONGP(cp, size); |
| 531 | prevdiff = val - prevval; |
| 532 | |
| 533 | for ( i=size; i<len; i+= size) { |
| 534 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 535 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 536 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 537 | diff = val - prevval; |
| 538 | if ( diff*prevdiff < 0 ) { |
| 539 | /* Derivative changed sign. Compute difference to last |
| 540 | ** extreme value and remember. |
| 541 | */ |
| 542 | if ( prevextremevalid ) { |
| 543 | extremediff = prevval - prevextreme; |
| 544 | if ( extremediff < 0 ) |
| 545 | extremediff = -extremediff; |
| 546 | avg += extremediff; |
| 547 | nextreme++; |
| 548 | } |
| 549 | prevextremevalid = 1; |
| 550 | prevextreme = prevval; |
| 551 | } |
| 552 | prevval = val; |
| 553 | if ( diff != 0 ) |
| 554 | prevdiff = diff; |
| 555 | } |
| 556 | if ( nextreme == 0 ) |
| 557 | val = 0; |
| 558 | else |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 559 | val = (int)(avg / (double)nextreme); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 560 | return PyInt_FromLong(val); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 563 | static PyObject * |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 564 | audioop_maxpp(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 565 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 566 | PyObject *args; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 567 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 568 | signed char *cp; |
| 569 | int len, size, val = 0, prevval = 0, prevextremevalid = 0, |
| 570 | prevextreme = 0; |
| 571 | int i; |
| 572 | int max = 0; |
| 573 | int diff, prevdiff, extremediff; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 574 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 575 | if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) ) |
| 576 | return 0; |
| 577 | if ( size != 1 && size != 2 && size != 4 ) { |
| 578 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 579 | return 0; |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 580 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 581 | /* Compute first delta value ahead. Also automatically makes us |
| 582 | ** skip the first extreme value |
| 583 | */ |
| 584 | if ( size == 1 ) prevval = (int)*CHARP(cp, 0); |
| 585 | else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0); |
| 586 | else if ( size == 4 ) prevval = (int)*LONGP(cp, 0); |
| 587 | if ( size == 1 ) val = (int)*CHARP(cp, size); |
| 588 | else if ( size == 2 ) val = (int)*SHORTP(cp, size); |
| 589 | else if ( size == 4 ) val = (int)*LONGP(cp, size); |
| 590 | prevdiff = val - prevval; |
| 591 | |
| 592 | for ( i=size; i<len; i+= size) { |
| 593 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 594 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 595 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 596 | diff = val - prevval; |
| 597 | if ( diff*prevdiff < 0 ) { |
| 598 | /* Derivative changed sign. Compute difference to |
| 599 | ** last extreme value and remember. |
| 600 | */ |
| 601 | if ( prevextremevalid ) { |
| 602 | extremediff = prevval - prevextreme; |
| 603 | if ( extremediff < 0 ) |
| 604 | extremediff = -extremediff; |
| 605 | if ( extremediff > max ) |
| 606 | max = extremediff; |
| 607 | } |
| 608 | prevextremevalid = 1; |
| 609 | prevextreme = prevval; |
| 610 | } |
| 611 | prevval = val; |
| 612 | if ( diff != 0 ) |
| 613 | prevdiff = diff; |
| 614 | } |
| 615 | return PyInt_FromLong(max); |
Jack Jansen | e1b4d7c | 1992-08-24 14:36:31 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 618 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 619 | audioop_cross(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 620 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 621 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 622 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 623 | signed char *cp; |
| 624 | int len, size, val = 0; |
| 625 | int i; |
| 626 | int prevval, ncross; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 627 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 628 | if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) ) |
| 629 | return 0; |
| 630 | if ( size != 1 && size != 2 && size != 4 ) { |
| 631 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 632 | return 0; |
| 633 | } |
| 634 | ncross = -1; |
| 635 | prevval = 17; /* Anything <> 0,1 */ |
| 636 | for ( i=0; i<len; i+= size) { |
| 637 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7; |
| 638 | else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15; |
| 639 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31; |
| 640 | val = val & 1; |
| 641 | if ( val != prevval ) ncross++; |
| 642 | prevval = val; |
| 643 | } |
| 644 | return PyInt_FromLong(ncross); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 647 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 648 | audioop_mul(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 649 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 650 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 651 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 652 | signed char *cp, *ncp; |
| 653 | int len, size, val = 0; |
| 654 | double factor, fval, maxval; |
| 655 | PyObject *rv; |
| 656 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 657 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 658 | if ( !PyArg_Parse(args, "(s#id)", &cp, &len, &size, &factor ) ) |
| 659 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 660 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 661 | if ( size == 1 ) maxval = (double) 0x7f; |
| 662 | else if ( size == 2 ) maxval = (double) 0x7fff; |
| 663 | else if ( size == 4 ) maxval = (double) 0x7fffffff; |
| 664 | else { |
| 665 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 666 | return 0; |
| 667 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 668 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 669 | rv = PyString_FromStringAndSize(NULL, len); |
| 670 | if ( rv == 0 ) |
| 671 | return 0; |
| 672 | ncp = (signed char *)PyString_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 673 | |
| 674 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 675 | for ( i=0; i < len; i += size ) { |
| 676 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 677 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 678 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
| 679 | fval = (double)val*factor; |
| 680 | if ( fval > maxval ) fval = maxval; |
| 681 | else if ( fval < -maxval ) fval = -maxval; |
| 682 | val = (int)fval; |
| 683 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)val; |
| 684 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)val; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 685 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 686 | } |
| 687 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 690 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 691 | audioop_tomono(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 692 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 693 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 694 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 695 | signed char *cp, *ncp; |
| 696 | int len, size, val1 = 0, val2 = 0; |
| 697 | double fac1, fac2, fval, maxval; |
| 698 | PyObject *rv; |
| 699 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 700 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 701 | if ( !PyArg_Parse(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) ) |
| 702 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 703 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 704 | if ( size == 1 ) maxval = (double) 0x7f; |
| 705 | else if ( size == 2 ) maxval = (double) 0x7fff; |
| 706 | else if ( size == 4 ) maxval = (double) 0x7fffffff; |
| 707 | else { |
| 708 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 709 | return 0; |
| 710 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 711 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 712 | rv = PyString_FromStringAndSize(NULL, len/2); |
| 713 | if ( rv == 0 ) |
| 714 | return 0; |
| 715 | ncp = (signed char *)PyString_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 716 | |
| 717 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 718 | for ( i=0; i < len; i += size*2 ) { |
| 719 | if ( size == 1 ) val1 = (int)*CHARP(cp, i); |
| 720 | else if ( size == 2 ) val1 = (int)*SHORTP(cp, i); |
| 721 | else if ( size == 4 ) val1 = (int)*LONGP(cp, i); |
| 722 | if ( size == 1 ) val2 = (int)*CHARP(cp, i+1); |
| 723 | else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2); |
| 724 | else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4); |
| 725 | fval = (double)val1*fac1 + (double)val2*fac2; |
| 726 | if ( fval > maxval ) fval = maxval; |
| 727 | else if ( fval < -maxval ) fval = -maxval; |
| 728 | val1 = (int)fval; |
| 729 | if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1; |
| 730 | else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 731 | else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 732 | } |
| 733 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 736 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 737 | audioop_tostereo(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 738 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 739 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 740 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 741 | signed char *cp, *ncp; |
| 742 | int len, size, val1, val2, val = 0; |
| 743 | double fac1, fac2, fval, maxval; |
| 744 | PyObject *rv; |
| 745 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 746 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 747 | if ( !PyArg_Parse(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) ) |
| 748 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 749 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 750 | if ( size == 1 ) maxval = (double) 0x7f; |
| 751 | else if ( size == 2 ) maxval = (double) 0x7fff; |
| 752 | else if ( size == 4 ) maxval = (double) 0x7fffffff; |
| 753 | else { |
| 754 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 755 | return 0; |
| 756 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 757 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 758 | rv = PyString_FromStringAndSize(NULL, len*2); |
| 759 | if ( rv == 0 ) |
| 760 | return 0; |
| 761 | ncp = (signed char *)PyString_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 762 | |
| 763 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 764 | for ( i=0; i < len; i += size ) { |
| 765 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 766 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 767 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 768 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 769 | fval = (double)val*fac1; |
| 770 | if ( fval > maxval ) fval = maxval; |
| 771 | else if ( fval < -maxval ) fval = -maxval; |
| 772 | val1 = (int)fval; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 773 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 774 | fval = (double)val*fac2; |
| 775 | if ( fval > maxval ) fval = maxval; |
| 776 | else if ( fval < -maxval ) fval = -maxval; |
| 777 | val2 = (int)fval; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 778 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 779 | if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1; |
| 780 | else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 781 | else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 782 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 783 | if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2; |
| 784 | else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 785 | else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 786 | } |
| 787 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 790 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 791 | audioop_add(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 792 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 793 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 794 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 795 | signed char *cp1, *cp2, *ncp; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 796 | int len1, len2, size, val1 = 0, val2 = 0, maxval, newval; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 797 | PyObject *rv; |
| 798 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 799 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 800 | if ( !PyArg_Parse(args, "(s#s#i)", |
| 801 | &cp1, &len1, &cp2, &len2, &size ) ) |
| 802 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 803 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 804 | if ( len1 != len2 ) { |
| 805 | PyErr_SetString(AudioopError, "Lengths should be the same"); |
| 806 | return 0; |
| 807 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 808 | |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 809 | if ( size == 1 ) maxval = 0x7f; |
| 810 | else if ( size == 2 ) maxval = 0x7fff; |
| 811 | else if ( size == 4 ) maxval = 0x7fffffff; |
| 812 | else { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 813 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 814 | return 0; |
| 815 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 816 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 817 | rv = PyString_FromStringAndSize(NULL, len1); |
| 818 | if ( rv == 0 ) |
| 819 | return 0; |
| 820 | ncp = (signed char *)PyString_AsString(rv); |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 821 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 822 | for ( i=0; i < len1; i += size ) { |
| 823 | if ( size == 1 ) val1 = (int)*CHARP(cp1, i); |
| 824 | else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i); |
| 825 | else if ( size == 4 ) val1 = (int)*LONGP(cp1, i); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 826 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 827 | if ( size == 1 ) val2 = (int)*CHARP(cp2, i); |
| 828 | else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i); |
| 829 | else if ( size == 4 ) val2 = (int)*LONGP(cp2, i); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 831 | newval = val1 + val2; |
| 832 | /* truncate in case of overflow */ |
| 833 | if (newval > maxval) newval = maxval; |
| 834 | else if (newval < -maxval) newval = -maxval; |
| 835 | else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0) |
| 836 | newval = val1 > 0 ? maxval : - maxval; |
| 837 | |
| 838 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval; |
| 839 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 840 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 841 | } |
| 842 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 845 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 846 | audioop_bias(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 847 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 848 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 849 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 850 | signed char *cp, *ncp; |
| 851 | int len, size, val = 0; |
| 852 | PyObject *rv; |
| 853 | int i; |
| 854 | int bias; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 855 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 856 | if ( !PyArg_Parse(args, "(s#ii)", |
| 857 | &cp, &len, &size , &bias) ) |
| 858 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 859 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 860 | if ( size != 1 && size != 2 && size != 4) { |
| 861 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 862 | return 0; |
| 863 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 864 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 865 | rv = PyString_FromStringAndSize(NULL, len); |
| 866 | if ( rv == 0 ) |
| 867 | return 0; |
| 868 | ncp = (signed char *)PyString_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 869 | |
| 870 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 871 | for ( i=0; i < len; i += size ) { |
| 872 | if ( size == 1 ) val = (int)*CHARP(cp, i); |
| 873 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 874 | else if ( size == 4 ) val = (int)*LONGP(cp, i); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 875 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 876 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias); |
| 877 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias); |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 878 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 879 | } |
| 880 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 883 | static PyObject * |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 884 | audioop_reverse(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 885 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 886 | PyObject *args; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 887 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 888 | signed char *cp; |
| 889 | unsigned char *ncp; |
| 890 | int len, size, val = 0; |
| 891 | PyObject *rv; |
| 892 | int i, j; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 893 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 894 | if ( !PyArg_Parse(args, "(s#i)", |
| 895 | &cp, &len, &size) ) |
| 896 | return 0; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 897 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 898 | if ( size != 1 && size != 2 && size != 4 ) { |
| 899 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 900 | return 0; |
| 901 | } |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 902 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 903 | rv = PyString_FromStringAndSize(NULL, len); |
| 904 | if ( rv == 0 ) |
| 905 | return 0; |
| 906 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 907 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 908 | for ( i=0; i < len; i += size ) { |
| 909 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 910 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 911 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 912 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 913 | j = len - i - size; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 914 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 915 | if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8); |
| 916 | else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val); |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 917 | else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 918 | } |
| 919 | return rv; |
Jack Jansen | 337b20e | 1993-02-23 13:39:57 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 922 | static PyObject * |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 923 | audioop_lin2lin(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 924 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 925 | PyObject *args; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 926 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 927 | signed char *cp; |
| 928 | unsigned char *ncp; |
| 929 | int len, size, size2, val = 0; |
| 930 | PyObject *rv; |
| 931 | int i, j; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 932 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 933 | if ( !PyArg_Parse(args, "(s#ii)", |
| 934 | &cp, &len, &size, &size2) ) |
| 935 | return 0; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 936 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 937 | if ( (size != 1 && size != 2 && size != 4) || |
| 938 | (size2 != 1 && size2 != 2 && size2 != 4)) { |
| 939 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 940 | return 0; |
| 941 | } |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 942 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 943 | rv = PyString_FromStringAndSize(NULL, (len/size)*size2); |
| 944 | if ( rv == 0 ) |
| 945 | return 0; |
| 946 | ncp = (unsigned char *)PyString_AsString(rv); |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 947 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 948 | for ( i=0, j=0; i < len; i += size, j += size2 ) { |
| 949 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 950 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 951 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 952 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 953 | if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8); |
| 954 | else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val); |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 955 | else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 956 | } |
| 957 | return rv; |
Jack Jansen | a90805f | 1993-02-17 14:29:28 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 960 | static int |
| 961 | gcd(a, b) |
| 962 | int a, b; |
| 963 | { |
| 964 | while (b > 0) { |
| 965 | int tmp = a % b; |
| 966 | a = b; |
| 967 | b = tmp; |
| 968 | } |
| 969 | return a; |
| 970 | } |
| 971 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 972 | static PyObject * |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 973 | audioop_ratecv(self, args) |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 974 | PyObject *self; |
| 975 | PyObject *args; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 976 | { |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 977 | char *cp, *ncp; |
| 978 | int len, size, nchannels, inrate, outrate, weightA, weightB; |
| 979 | int chan, d, *prev_i, *cur_i, cur_o; |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 980 | PyObject *state, *samps, *str, *rv = NULL; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 981 | |
| 982 | weightA = 1; |
| 983 | weightB = 0; |
| 984 | if (!PyArg_ParseTuple(args, "s#iiiiO|ii", &cp, &len, &size, &nchannels, |
| 985 | &inrate, &outrate, &state, &weightA, &weightB)) |
| 986 | return NULL; |
| 987 | if (size != 1 && size != 2 && size != 4) { |
| 988 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 989 | return NULL; |
| 990 | } |
| 991 | if (nchannels < 1) { |
| 992 | PyErr_SetString(AudioopError, "# of channels should be >= 1"); |
| 993 | return NULL; |
| 994 | } |
| 995 | if (weightA < 1 || weightB < 0) { |
| 996 | PyErr_SetString(AudioopError, |
| 997 | "weightA should be >= 1, weightB should be >= 0"); |
| 998 | return NULL; |
| 999 | } |
| 1000 | if (len % (size * nchannels) != 0) { |
| 1001 | PyErr_SetString(AudioopError, "not a whole number of frames"); |
| 1002 | return NULL; |
| 1003 | } |
Guido van Rossum | b24c9ea | 1997-05-20 15:59:35 +0000 | [diff] [blame] | 1004 | if (inrate <= 0 || outrate <= 0) { |
| 1005 | PyErr_SetString(AudioopError, "sampling rate not > 0"); |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | /* divide inrate and outrate by their greatest common divisor */ |
| 1009 | d = gcd(inrate, outrate); |
| 1010 | inrate /= d; |
| 1011 | outrate /= d; |
| 1012 | |
Guido van Rossum | 6345ac6 | 1997-10-31 20:32:13 +0000 | [diff] [blame] | 1013 | prev_i = (int *) malloc(nchannels * sizeof(int)); |
| 1014 | cur_i = (int *) malloc(nchannels * sizeof(int)); |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1015 | len /= size * nchannels; /* # of frames */ |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1016 | if (prev_i == NULL || cur_i == NULL) { |
| 1017 | (void) PyErr_NoMemory(); |
| 1018 | goto exit; |
| 1019 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1020 | |
| 1021 | if (state == Py_None) { |
| 1022 | d = -outrate; |
| 1023 | for (chan = 0; chan < nchannels; chan++) |
| 1024 | prev_i[chan] = cur_i[chan] = 0; |
| 1025 | } else { |
| 1026 | if (!PyArg_ParseTuple(state, |
| 1027 | "iO!;audioop.ratecv: illegal state argument", |
| 1028 | &d, &PyTuple_Type, &samps)) |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1029 | goto exit; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1030 | if (PyTuple_Size(samps) != nchannels) { |
| 1031 | PyErr_SetString(AudioopError, |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1032 | "illegal state argument"); |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1033 | goto exit; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1034 | } |
| 1035 | for (chan = 0; chan < nchannels; chan++) { |
| 1036 | if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan), |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1037 | "ii",&prev_i[chan],&cur_i[chan])) |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1038 | goto exit; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | str = PyString_FromStringAndSize( |
| 1042 | NULL, size * nchannels * (len * outrate + inrate - 1) / inrate); |
| 1043 | if (str == NULL) |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1044 | goto exit; |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1045 | ncp = PyString_AsString(str); |
| 1046 | |
| 1047 | for (;;) { |
| 1048 | while (d < 0) { |
| 1049 | if (len == 0) { |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1050 | samps = PyTuple_New(nchannels); |
| 1051 | for (chan = 0; chan < nchannels; chan++) |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1052 | PyTuple_SetItem(samps, chan, |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1053 | Py_BuildValue("(ii)", |
| 1054 | prev_i[chan], |
| 1055 | cur_i[chan])); |
| 1056 | if (PyErr_Occurred()) |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1057 | goto exit; |
Guido van Rossum | 3bbeb7a | 1997-09-22 16:14:27 +0000 | [diff] [blame] | 1058 | len = ncp - PyString_AsString(str); |
| 1059 | if (len == 0) { |
| 1060 | /*don't want to resize to zero length*/ |
| 1061 | rv = PyString_FromStringAndSize("", 0); |
| 1062 | Py_DECREF(str); |
| 1063 | str = rv; |
| 1064 | } else if (_PyString_Resize(&str, len) < 0) |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1065 | goto exit; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1066 | rv = Py_BuildValue("(O(iO))", str, d, samps); |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1067 | Py_DECREF(samps); |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1068 | Py_DECREF(str); |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1069 | goto exit; /* return rv */ |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1070 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1071 | for (chan = 0; chan < nchannels; chan++) { |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1072 | prev_i[chan] = cur_i[chan]; |
| 1073 | if (size == 1) |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1074 | cur_i[chan] = ((int)*CHARP(cp, 0)) << 8; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1075 | else if (size == 2) |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1076 | cur_i[chan] = (int)*SHORTP(cp, 0); |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1077 | else if (size == 4) |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1078 | cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1079 | cp += size; |
| 1080 | /* implements a simple digital filter */ |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1081 | cur_i[chan] = |
| 1082 | (weightA * cur_i[chan] + |
| 1083 | weightB * prev_i[chan]) / |
| 1084 | (weightA + weightB); |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1085 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1086 | len--; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1087 | d += outrate; |
| 1088 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1089 | while (d >= 0) { |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1090 | for (chan = 0; chan < nchannels; chan++) { |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1091 | cur_o = (prev_i[chan] * d + |
| 1092 | cur_i[chan] * (outrate - d)) / |
| 1093 | outrate; |
| 1094 | if (size == 1) |
| 1095 | *CHARP(ncp, 0) = (signed char)(cur_o >> 8); |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1096 | else if (size == 2) |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1097 | *SHORTP(ncp, 0) = (short)(cur_o); |
| 1098 | else if (size == 4) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 1099 | *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16); |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1100 | ncp += size; |
| 1101 | } |
| 1102 | d -= inrate; |
| 1103 | } |
| 1104 | } |
Guido van Rossum | 65bb328 | 1999-09-07 14:24:05 +0000 | [diff] [blame^] | 1105 | exit: |
| 1106 | if (prev_i != NULL) |
| 1107 | free(prev_i); |
| 1108 | if (cur_i != NULL) |
| 1109 | free(cur_i); |
| 1110 | return rv; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1111 | } |
Guido van Rossum | 1851a67 | 1997-02-14 16:14:03 +0000 | [diff] [blame] | 1112 | |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1113 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1114 | audioop_lin2ulaw(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1115 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1116 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1117 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1118 | signed char *cp; |
| 1119 | unsigned char *ncp; |
| 1120 | int len, size, val = 0; |
| 1121 | PyObject *rv; |
| 1122 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1123 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1124 | if ( !PyArg_Parse(args, "(s#i)", |
| 1125 | &cp, &len, &size) ) |
| 1126 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1127 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1128 | if ( size != 1 && size != 2 && size != 4) { |
| 1129 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 1130 | return 0; |
| 1131 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1132 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1133 | rv = PyString_FromStringAndSize(NULL, len/size); |
| 1134 | if ( rv == 0 ) |
| 1135 | return 0; |
| 1136 | ncp = (unsigned char *)PyString_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1137 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1138 | for ( i=0; i < len; i += size ) { |
| 1139 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1140 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1141 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1142 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1143 | *ncp++ = st_linear_to_ulaw(val); |
| 1144 | } |
| 1145 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1148 | static PyObject * |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1149 | audioop_ulaw2lin(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1150 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1151 | PyObject *args; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1152 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1153 | unsigned char *cp; |
| 1154 | unsigned char cval; |
| 1155 | signed char *ncp; |
| 1156 | int len, size, val; |
| 1157 | PyObject *rv; |
| 1158 | int i; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1159 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1160 | if ( !PyArg_Parse(args, "(s#i)", |
| 1161 | &cp, &len, &size) ) |
| 1162 | return 0; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1163 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1164 | if ( size != 1 && size != 2 && size != 4) { |
| 1165 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 1166 | return 0; |
| 1167 | } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1168 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1169 | rv = PyString_FromStringAndSize(NULL, len*size); |
| 1170 | if ( rv == 0 ) |
| 1171 | return 0; |
| 1172 | ncp = (signed char *)PyString_AsString(rv); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1173 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1174 | for ( i=0; i < len*size; i += size ) { |
| 1175 | cval = *cp++; |
| 1176 | val = st_ulaw_to_linear(cval); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1177 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1178 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8); |
| 1179 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val); |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 1180 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16); |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1181 | } |
| 1182 | return rv; |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1185 | static PyObject * |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1186 | audioop_lin2adpcm(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1187 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1188 | PyObject *args; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1189 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1190 | signed char *cp; |
| 1191 | signed char *ncp; |
| 1192 | int len, size, val = 0, step, valpred, delta, |
| 1193 | index, sign, vpdiff, diff; |
| 1194 | PyObject *rv, *state, *str; |
| 1195 | int i, outputbuffer = 0, bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1196 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1197 | if ( !PyArg_Parse(args, "(s#iO)", |
| 1198 | &cp, &len, &size, &state) ) |
| 1199 | return 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1200 | |
| 1201 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1202 | if ( size != 1 && size != 2 && size != 4) { |
| 1203 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 1204 | return 0; |
| 1205 | } |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1206 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1207 | str = PyString_FromStringAndSize(NULL, len/(size*2)); |
| 1208 | if ( str == 0 ) |
| 1209 | return 0; |
| 1210 | ncp = (signed char *)PyString_AsString(str); |
Jack Jansen | dd8a6ea | 1993-02-17 14:21:09 +0000 | [diff] [blame] | 1211 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1212 | /* Decode state, should have (value, step) */ |
| 1213 | if ( state == Py_None ) { |
| 1214 | /* First time, it seems. Set defaults */ |
| 1215 | valpred = 0; |
| 1216 | step = 7; |
| 1217 | index = 0; |
| 1218 | } else if ( !PyArg_Parse(state, "(ii)", &valpred, &index) ) |
| 1219 | return 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1220 | |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1221 | step = stepsizeTable[index]; |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1222 | bufferstep = 1; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1223 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1224 | for ( i=0; i < len; i += size ) { |
| 1225 | if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8; |
| 1226 | else if ( size == 2 ) val = (int)*SHORTP(cp, i); |
| 1227 | else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16; |
| 1228 | |
| 1229 | /* Step 1 - compute difference with previous value */ |
| 1230 | diff = val - valpred; |
| 1231 | sign = (diff < 0) ? 8 : 0; |
| 1232 | if ( sign ) diff = (-diff); |
| 1233 | |
| 1234 | /* Step 2 - Divide and clamp */ |
| 1235 | /* Note: |
| 1236 | ** This code *approximately* computes: |
| 1237 | ** delta = diff*4/step; |
| 1238 | ** vpdiff = (delta+0.5)*step/4; |
| 1239 | ** but in shift step bits are dropped. The net result of this |
| 1240 | ** is that even if you have fast mul/div hardware you cannot |
| 1241 | ** put it to good use since the fixup would be too expensive. |
| 1242 | */ |
| 1243 | delta = 0; |
| 1244 | vpdiff = (step >> 3); |
| 1245 | |
| 1246 | if ( diff >= step ) { |
| 1247 | delta = 4; |
| 1248 | diff -= step; |
| 1249 | vpdiff += step; |
| 1250 | } |
| 1251 | step >>= 1; |
| 1252 | if ( diff >= step ) { |
| 1253 | delta |= 2; |
| 1254 | diff -= step; |
| 1255 | vpdiff += step; |
| 1256 | } |
| 1257 | step >>= 1; |
| 1258 | if ( diff >= step ) { |
| 1259 | delta |= 1; |
| 1260 | vpdiff += step; |
| 1261 | } |
| 1262 | |
| 1263 | /* Step 3 - Update previous value */ |
| 1264 | if ( sign ) |
| 1265 | valpred -= vpdiff; |
| 1266 | else |
| 1267 | valpred += vpdiff; |
| 1268 | |
| 1269 | /* Step 4 - Clamp previous value to 16 bits */ |
| 1270 | if ( valpred > 32767 ) |
| 1271 | valpred = 32767; |
| 1272 | else if ( valpred < -32768 ) |
| 1273 | valpred = -32768; |
| 1274 | |
| 1275 | /* Step 5 - Assemble value, update index and step values */ |
| 1276 | delta |= sign; |
| 1277 | |
| 1278 | index += indexTable[delta]; |
| 1279 | if ( index < 0 ) index = 0; |
| 1280 | if ( index > 88 ) index = 88; |
| 1281 | step = stepsizeTable[index]; |
| 1282 | |
| 1283 | /* Step 6 - Output value */ |
| 1284 | if ( bufferstep ) { |
| 1285 | outputbuffer = (delta << 4) & 0xf0; |
| 1286 | } else { |
| 1287 | *ncp++ = (delta & 0x0f) | outputbuffer; |
| 1288 | } |
| 1289 | bufferstep = !bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1290 | } |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1291 | rv = Py_BuildValue("(O(ii))", str, valpred, index); |
| 1292 | Py_DECREF(str); |
| 1293 | return rv; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1296 | static PyObject * |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1297 | audioop_adpcm2lin(self, args) |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1298 | PyObject *self; |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1299 | PyObject *args; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1300 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1301 | signed char *cp; |
| 1302 | signed char *ncp; |
| 1303 | int len, size, valpred, step, delta, index, sign, vpdiff; |
| 1304 | PyObject *rv, *str, *state; |
| 1305 | int i, inputbuffer = 0, bufferstep; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1306 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1307 | if ( !PyArg_Parse(args, "(s#iO)", |
| 1308 | &cp, &len, &size, &state) ) |
| 1309 | return 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1310 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1311 | if ( size != 1 && size != 2 && size != 4) { |
| 1312 | PyErr_SetString(AudioopError, "Size should be 1, 2 or 4"); |
| 1313 | return 0; |
| 1314 | } |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1315 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1316 | /* Decode state, should have (value, step) */ |
| 1317 | if ( state == Py_None ) { |
| 1318 | /* First time, it seems. Set defaults */ |
| 1319 | valpred = 0; |
| 1320 | step = 7; |
| 1321 | index = 0; |
| 1322 | } else if ( !PyArg_Parse(state, "(ii)", &valpred, &index) ) |
| 1323 | return 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1324 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1325 | str = PyString_FromStringAndSize(NULL, len*size*2); |
| 1326 | if ( str == 0 ) |
| 1327 | return 0; |
| 1328 | ncp = (signed char *)PyString_AsString(str); |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1329 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1330 | step = stepsizeTable[index]; |
| 1331 | bufferstep = 0; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1332 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1333 | for ( i=0; i < len*size*2; i += size ) { |
| 1334 | /* Step 1 - get the delta value and compute next index */ |
| 1335 | if ( bufferstep ) { |
| 1336 | delta = inputbuffer & 0xf; |
| 1337 | } else { |
| 1338 | inputbuffer = *cp++; |
| 1339 | delta = (inputbuffer >> 4) & 0xf; |
| 1340 | } |
| 1341 | |
| 1342 | bufferstep = !bufferstep; |
| 1343 | |
| 1344 | /* Step 2 - Find new index value (for later) */ |
| 1345 | index += indexTable[delta]; |
| 1346 | if ( index < 0 ) index = 0; |
| 1347 | if ( index > 88 ) index = 88; |
| 1348 | |
| 1349 | /* Step 3 - Separate sign and magnitude */ |
| 1350 | sign = delta & 8; |
| 1351 | delta = delta & 7; |
| 1352 | |
| 1353 | /* Step 4 - Compute difference and new predicted value */ |
| 1354 | /* |
| 1355 | ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment |
| 1356 | ** in adpcm_coder. |
| 1357 | */ |
| 1358 | vpdiff = step >> 3; |
| 1359 | if ( delta & 4 ) vpdiff += step; |
| 1360 | if ( delta & 2 ) vpdiff += step>>1; |
| 1361 | if ( delta & 1 ) vpdiff += step>>2; |
| 1362 | |
| 1363 | if ( sign ) |
| 1364 | valpred -= vpdiff; |
| 1365 | else |
| 1366 | valpred += vpdiff; |
| 1367 | |
| 1368 | /* Step 5 - clamp output value */ |
| 1369 | if ( valpred > 32767 ) |
| 1370 | valpred = 32767; |
| 1371 | else if ( valpred < -32768 ) |
| 1372 | valpred = -32768; |
| 1373 | |
| 1374 | /* Step 6 - Update step value */ |
| 1375 | step = stepsizeTable[index]; |
| 1376 | |
| 1377 | /* Step 6 - Output value */ |
| 1378 | if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8); |
| 1379 | else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred); |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 1380 | else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16); |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1383 | rv = Py_BuildValue("(O(ii))", str, valpred, index); |
| 1384 | Py_DECREF(str); |
| 1385 | return rv; |
Guido van Rossum | b64e635 | 1992-07-06 14:21:56 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1388 | static PyMethodDef audioop_methods[] = { |
| 1389 | { "max", audioop_max }, |
| 1390 | { "minmax", audioop_minmax }, |
| 1391 | { "avg", audioop_avg }, |
| 1392 | { "maxpp", audioop_maxpp }, |
| 1393 | { "avgpp", audioop_avgpp }, |
| 1394 | { "rms", audioop_rms }, |
| 1395 | { "findfit", audioop_findfit }, |
| 1396 | { "findmax", audioop_findmax }, |
| 1397 | { "findfactor", audioop_findfactor }, |
| 1398 | { "cross", audioop_cross }, |
| 1399 | { "mul", audioop_mul }, |
| 1400 | { "add", audioop_add }, |
| 1401 | { "bias", audioop_bias }, |
| 1402 | { "ulaw2lin", audioop_ulaw2lin }, |
| 1403 | { "lin2ulaw", audioop_lin2ulaw }, |
| 1404 | { "lin2lin", audioop_lin2lin }, |
| 1405 | { "adpcm2lin", audioop_adpcm2lin }, |
| 1406 | { "lin2adpcm", audioop_lin2adpcm }, |
| 1407 | { "tomono", audioop_tomono }, |
| 1408 | { "tostereo", audioop_tostereo }, |
| 1409 | { "getsample", audioop_getsample }, |
| 1410 | { "reverse", audioop_reverse }, |
Roger E. Masse | c905fff | 1997-01-17 18:12:04 +0000 | [diff] [blame] | 1411 | { "ratecv", audioop_ratecv, 1 }, |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1412 | { 0, 0 } |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1413 | }; |
| 1414 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 1415 | DL_EXPORT(void) |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1416 | initaudioop() |
| 1417 | { |
Roger E. Masse | eaa6e11 | 1997-01-03 19:26:27 +0000 | [diff] [blame] | 1418 | PyObject *m, *d; |
| 1419 | m = Py_InitModule("audioop", audioop_methods); |
| 1420 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 1421 | AudioopError = PyErr_NewException("audioop.error", NULL, NULL); |
| 1422 | if (AudioopError != NULL) |
| 1423 | PyDict_SetItemString(d,"error",AudioopError); |
Guido van Rossum | b66efa0 | 1992-06-01 16:01:24 +0000 | [diff] [blame] | 1424 | } |