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