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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 All Rights Reserved
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ******************************************************************/
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
2506
9d6e97d66670 changes for MPW
Guido van Rossum <guido@python.org>
parents: 2150
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 #define MINLIN -32768
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 #define MAXLIN 32767
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 #define LINCLIP(x) do { if ( x < MINLIN ) x = MINLIN ; else if ( x > MAXLIN ) x = MAXLIN; } while ( 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 /*
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 ** This macro converts from ulaw to 16 bit linear, faster.
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 **
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 ** Jef Poskanzer
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 ** 23 October 1989
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 **
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 ** Input: 8 bit ulaw sample
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 ** Output: signed 16 bit linear sample
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #define st_ulaw_to_linear(ulawbyte) ulaw_table[ulawbyte]
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 static int ulaw_table[256] = {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 -876, -844, -812, -780, -748, -716, -684, -652,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 -620, -588, -556, -524, -492, -460, -428, -396,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 -372, -356, -340, -324, -308, -292, -276, -260,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 -244, -228, -212, -196, -180, -164, -148, -132,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 -120, -112, -104, -96, -88, -80, -72, -64,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 -56, -48, -40, -32, -24, -16, -8, 0,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 876, 844, 812, 780, 748, 716, 684, 652,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 620, 588, 556, 524, 492, 460, 428, 396,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 372, 356, 340, 324, 308, 292, 276, 260,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 244, 228, 212, 196, 180, 164, 148, 132,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 120, 112, 104, 96, 88, 80, 72, 64,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 56, 48, 40, 32, 24, 16, 8, 0 };
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 #define CLIP 32635
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 st_linear_to_ulaw( sample )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 int sample;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 int sign, exponent, mantissa;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 unsigned char ulawbyte;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 /* Get the sample into sign-magnitude. */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 sign = (sample >> 8) & 0x80; /* set aside the sign */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 if ( sign != 0 ) sample = -sample; /* get magnitude */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 if ( sample > CLIP ) sample = CLIP; /* clip the magnitude */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 /* Convert from 16 bit linear to ulaw. */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 sample = sample + BIAS;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 exponent = exp_lut[( sample >> 7 ) & 0xFF];
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 #ifdef ZEROTRAP
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 #endif
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 return ulawbyte;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 /* End of code taken from sox */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 #define CHARP(cp, i) ((signed char *)(cp+i))
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 #define SHORTP(cp, i) ((short *)(cp+i))
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 #define LONGP(cp, i) ((long *)(cp+i))
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 static object *AudioopError;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 audioop_getsample(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 signed char *cp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 if ( !getargs(args, "(s#ii)", &cp, &len, &size, &i) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 if ( size != 1 && size != 2 && size != 4 ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 if ( i < 0 || i >= len/size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 err_setstr(AudioopError, "Index out of range");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 if ( size == 1 ) val = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 return newintobject(val);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 audioop_max(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 signed char *cp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 int max = 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 if ( !getargs(args, "(s#i)", &cp, &len, &size) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 if ( size != 1 && size != 2 && size != 4 ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 for ( i=0; i<len; i+= size) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 if ( size == 1 ) val = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 else if ( size == 4 ) val = (int)*LONGP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 if ( val < 0 ) val = (-val);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 if ( val > max ) max = val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 return newintobject(max);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 static object *
1866
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
224 audioop_minmax(self, args)
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
225 object *self;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
226 object *args;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
227 {
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
228 signed char *cp;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
229 int len, size, val;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
230 int i;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
231 int min = 0x7fffffff, max = -0x7fffffff;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
232
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
233 if (!getargs(args, "(s#i)", &cp, &len, &size))
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
234 return NULL;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
235 if (size != 1 && size != 2 && size != 4) {
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
236 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
237 return NULL;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
238 }
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
239 for (i = 0; i < len; i += size) {
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
240 if (size == 1) val = (int) *CHARP(cp, i);
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
241 else if (size == 2) val = (int) *SHORTP(cp, i);
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
242 else if (size == 4) val = (int) *LONGP(cp, i);
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
243 if (val > max) max = val;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
244 if (val < min) min = val;
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
245 }
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
246 return mkvalue("(ii)", min, max);
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
247 }
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
248
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
249 static object *
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 audioop_avg(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 signed char *cp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 float avg = 0.0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 if ( !getargs(args, "(s#i)", &cp, &len, &size) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 if ( size != 1 && size != 2 && size != 4 ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 for ( i=0; i<len; i+= size) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 if ( size == 1 ) val = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 else if ( size == 4 ) val = (int)*LONGP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 avg += val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 if ( len == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 val = 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 else
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 val = (int)(avg / (float)(len/size));
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 return newintobject(val);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 audioop_cross(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 signed char *cp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
611
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 if ( !getargs(args, "(s#i)", &cp, &len, &size) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 if ( size != 1 && size != 2 && size != 4 ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 ncross = -1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 prevval = 17; /* Anything <> 0,1 */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 for ( i=0; i<len; i+= size) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 val = val & 1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 if ( val != prevval ) ncross++;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 prevval = val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 return newintobject(ncross);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 audioop_mul(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 signed char *cp, *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 double factor, fval, maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 if ( !getargs(args, "(s#id)", &cp, &len, &size, &factor ) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
644
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 if ( size == 1 ) maxval = (double) 0x7f;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 else if ( size == 2 ) maxval = (double) 0x7fff;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 else if ( size == 4 ) maxval = (double) 0x7fffffff;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 else {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
652
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 rv = newsizedstringobject(NULL, len);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 ncp = (signed char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
658
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 for ( i=0; i < len; i += size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 if ( size == 1 ) val = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 else if ( size == 4 ) val = (int)*LONGP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 fval = (double)val*factor;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 if ( fval > maxval ) fval = maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 else if ( fval < -maxval ) fval = -maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 val = (int)fval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 else if ( size == 4 ) *LONGP(ncp, i) = (long)val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
673
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 audioop_tomono(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 signed char *cp, *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 int len, size, val1, val2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 double fac1, fac2, fval, maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
683 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
684
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 if ( !getargs(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
687
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 if ( size == 1 ) maxval = (double) 0x7f;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 else if ( size == 2 ) maxval = (double) 0x7fff;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 else if ( size == 4 ) maxval = (double) 0x7fffffff;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 else {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
695
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 rv = newsizedstringobject(NULL, len/2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 ncp = (signed char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
700
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
701
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 for ( i=0; i < len; i += size*2 ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 if ( size == 1 ) val1 = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 fval = (double)val1*fac1 + (double)val2*fac2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 if ( fval > maxval ) fval = maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 else if ( fval < -maxval ) fval = -maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 val1 = (int)fval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 else if ( size == 4 ) *LONGP(ncp, i/2)= (long)val1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 audioop_tostereo(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 signed char *cp, *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 int len, size, val1, val2, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 double fac1, fac2, fval, maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
730
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 if ( !getargs(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
733
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 if ( size == 1 ) maxval = (double) 0x7f;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 else if ( size == 2 ) maxval = (double) 0x7fff;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 else if ( size == 4 ) maxval = (double) 0x7fffffff;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 else {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
741
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 rv = newsizedstringobject(NULL, len*2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 ncp = (signed char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
746
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
747
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 for ( i=0; i < len; i += size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 if ( size == 1 ) val = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
750 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
751 else if ( size == 4 ) val = (int)*LONGP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
752
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
753 fval = (double)val*fac1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 if ( fval > maxval ) fval = maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 else if ( fval < -maxval ) fval = -maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 val1 = (int)fval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
757
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 fval = (double)val*fac2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 if ( fval > maxval ) fval = maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 else if ( fval < -maxval ) fval = -maxval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
761 val2 = (int)fval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
762
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
764 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 else if ( size == 4 ) *LONGP(ncp, i*2) = (long)val1;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
766
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (long)val2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
771 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
772 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
773
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 audioop_add(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 signed char *cp1, *cp2, *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 int len1, len2, size, val1, val2;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
783
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 if ( !getargs(args, "(s#s#i)",
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 &cp1, &len1, &cp2, &len2, &size ) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
787
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 if ( len1 != len2 ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 err_setstr(AudioopError, "Lengths should be the same");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
790 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
792
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 if ( size != 1 && size != 2 && size != 4) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
797
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 rv = newsizedstringobject(NULL, len1);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 ncp = (signed char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
802
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 for ( i=0; i < len1; i += size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
808
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
812
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val1+val2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val1+val2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 else if ( size == 4 ) *LONGP(ncp, i) = (long)(val1+val2);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
819
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 audioop_bias(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
822 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
823 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
824 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 signed char *cp, *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
828 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 int bias;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
830
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
831 if ( !getargs(args, "(s#ii)",
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 &cp, &len, &size , &bias) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
833 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
834
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 if ( size != 1 && size != 2 && size != 4) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
838 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
839
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
840 rv = newsizedstringobject(NULL, len);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 ncp = (signed char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
844
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
845
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
846 for ( i=0; i < len; i += size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 if ( size == 1 ) val = (int)*CHARP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 else if ( size == 4 ) val = (int)*LONGP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
850
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
853 else if ( size == 4 ) *LONGP(ncp, i) = (long)(val+bias);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
855 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
857
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
936 audioop_lin2ulaw(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
938 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 signed char *cp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 unsigned char *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
944 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
945
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 if ( !getargs(args, "(s#i)",
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 &cp, &len, &size) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
949
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 if ( size != 1 && size != 2 && size != 4) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
952 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
953 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
954
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 rv = newsizedstringobject(NULL, len/size);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 ncp = (unsigned char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
959
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 for ( i=0; i < len; i += size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
961 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
963 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
964
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
965 *ncp++ = st_linear_to_ulaw(val);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
969
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 static object *
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 audioop_ulaw2lin(self, args)
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 object *self;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 object *args;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 unsigned char *cp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 unsigned char cval;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 signed char *ncp;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
978 int len, size, val;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 object *rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 int i;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
981
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 if ( !getargs(args, "(s#i)",
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 &cp, &len, &size) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
985
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 if ( size != 1 && size != 2 && size != 4) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 err_setstr(AudioopError, "Size should be 1, 2 or 4");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
988 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
990
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 rv = newsizedstringobject(NULL, len*size);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 if ( rv == 0 )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 return 0;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 ncp = (signed char *)getstringvalue(rv);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
995
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 for ( i=0; i < len*size; i += size ) {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 cval = *cp++;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 val = st_ulaw_to_linear(cval);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
999
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 return rv;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 static struct methodlist audioop_methods[] = {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 { "max", audioop_max },
1866
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
1211 { "minmax", audioop_minmax },
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 { "cross", audioop_cross },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220 { "mul", audioop_mul },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221 { "add", audioop_add },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 { "bias", audioop_bias },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 { "ulaw2lin", audioop_ulaw2lin },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 { "tomono", audioop_tomono },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 { "tostereo", audioop_tostereo },
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232 { 0, 0 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 };
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1236 void
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 initaudioop()
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1238 {
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 object *m, *d;
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 m = initmodule("audioop", audioop_methods);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241 d = getmoduledict(m);
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242 AudioopError = newstringobject("audioop.error");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 if ( AudioopError == NULL || dictinsert(d,"error",AudioopError) )
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1244 fatal("can't define audioop.error");
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1245 }