annotate Modules/audioop.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents bc943bbfdb1e
children e38335d4df91 4acd936cf6b6
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
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1420
diff changeset
2 /* 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
3
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
4 #include "Python.h"
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
6 #if SIZEOF_INT == 4
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
7 typedef int Py_Int32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
8 typedef unsigned int Py_UInt32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
9 #else
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
10 #if SIZEOF_LONG == 4
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
11 typedef long Py_Int32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
12 typedef unsigned long Py_UInt32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
13 #else
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
14 #error "No 4-byte integral type"
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
15 #endif
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
16 #endif
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
17
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
18 typedef short PyInt16;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
19
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
20 #if defined(__CHAR_UNSIGNED__)
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
21 #if defined(signed)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1420
diff changeset
22 /* 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
23 characters are available. Take it out of Setup. Sorry. */
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 #endif
1827
f5f3ddfec3c7 Makefile.pre.in: add $(OPT) to link command
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
25 #endif
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
27 /* Code shamelessly stolen from sox, 12.17.7, g711.c
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 ** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
30 /* From g711.c:
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
31 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
32 * December 30, 1994:
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
33 * Functions linear2alaw, linear2ulaw have been updated to correctly
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
34 * convert unquantized 16 bit values.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
35 * Tables for direct u- to A-law and A- to u-law conversions have been
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
36 * corrected.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
37 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
38 * bli@cpk.auc.dk
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
39 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
40 */
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
41 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
42 #define CLIP 32635
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
43 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
44 #define QUANT_MASK (0xf) /* Quantization field mask. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
45 #define SEG_SHIFT (4) /* Left shift for segment number. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
46 #define SEG_MASK (0x70) /* Segment field mask. */
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
47
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
48 static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
49 0x1FF, 0x3FF, 0x7FF, 0xFFF};
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
50 static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
51 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
52
37102
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
53 static PyInt16
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
54 search(PyInt16 val, PyInt16 *table, int size)
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
55 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
56 int i;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
57
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
58 for (i = 0; i < size; i++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
59 if (val <= *table++)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
60 return (i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
61 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
62 return (size);
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
63 }
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
64 #define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
65 #define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
37102
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
67 static PyInt16 _st_ulaw2linear16[256] = {
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
68 -32124, -31100, -30076, -29052, -28028, -27004, -25980,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
69 -24956, -23932, -22908, -21884, -20860, -19836, -18812,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
70 -17788, -16764, -15996, -15484, -14972, -14460, -13948,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
71 -13436, -12924, -12412, -11900, -11388, -10876, -10364,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
72 -9852, -9340, -8828, -8316, -7932, -7676, -7420,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
73 -7164, -6908, -6652, -6396, -6140, -5884, -5628,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
74 -5372, -5116, -4860, -4604, -4348, -4092, -3900,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
75 -3772, -3644, -3516, -3388, -3260, -3132, -3004,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
76 -2876, -2748, -2620, -2492, -2364, -2236, -2108,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
77 -1980, -1884, -1820, -1756, -1692, -1628, -1564,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
78 -1500, -1436, -1372, -1308, -1244, -1180, -1116,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
79 -1052, -988, -924, -876, -844, -812, -780,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
80 -748, -716, -684, -652, -620, -588, -556,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
81 -524, -492, -460, -428, -396, -372, -356,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
82 -340, -324, -308, -292, -276, -260, -244,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
83 -228, -212, -196, -180, -164, -148, -132,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
84 -120, -112, -104, -96, -88, -80, -72,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
85 -64, -56, -48, -40, -32, -24, -16,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
86 -8, 0, 32124, 31100, 30076, 29052, 28028,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
87 27004, 25980, 24956, 23932, 22908, 21884, 20860,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
88 19836, 18812, 17788, 16764, 15996, 15484, 14972,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
89 14460, 13948, 13436, 12924, 12412, 11900, 11388,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
90 10876, 10364, 9852, 9340, 8828, 8316, 7932,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
91 7676, 7420, 7164, 6908, 6652, 6396, 6140,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
92 5884, 5628, 5372, 5116, 4860, 4604, 4348,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
93 4092, 3900, 3772, 3644, 3516, 3388, 3260,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
94 3132, 3004, 2876, 2748, 2620, 2492, 2364,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
95 2236, 2108, 1980, 1884, 1820, 1756, 1692,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
96 1628, 1564, 1500, 1436, 1372, 1308, 1244,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
97 1180, 1116, 1052, 988, 924, 876, 844,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
98 812, 780, 748, 716, 684, 652, 620,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
99 588, 556, 524, 492, 460, 428, 396,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
100 372, 356, 340, 324, 308, 292, 276,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
101 260, 244, 228, 212, 196, 180, 164,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
102 148, 132, 120, 112, 104, 96, 88,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
103 80, 72, 64, 56, 48, 40, 32,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
104 24, 16, 8, 0
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
105 };
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 /*
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
108 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
109 * stored in a unsigned char. This function should only be called with
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
110 * the data shifted such that it only contains information in the lower
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
111 * 14-bits.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
112 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
113 * In order to simplify the encoding process, the original linear magnitude
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
114 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
115 * (33 - 8191). The result can be seen in the following encoding table:
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
116 *
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
117 * Biased Linear Input Code Compressed Code
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
118 * ------------------------ ---------------
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
119 * 00000001wxyza 000wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
120 * 0000001wxyzab 001wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
121 * 000001wxyzabc 010wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
122 * 00001wxyzabcd 011wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
123 * 0001wxyzabcde 100wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
124 * 001wxyzabcdef 101wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
125 * 01wxyzabcdefg 110wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
126 * 1wxyzabcdefgh 111wxyz
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
127 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
128 * Each biased linear code has a leading 1 which identifies the segment
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
129 * number. The value of the segment number is equal to 7 minus the number
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
130 * of leading 0's. The quantization interval is directly available as the
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
131 * four bits wxyz. * The trailing bits (a - h) are ignored.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
132 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
133 * Ordinarily the complement of the resulting code word is used for
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
134 * transmission, and so the code word is complemented before it is returned.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
135 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
136 * For further information see John C. Bellamy's Digital Telephony, 1982,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
137 * John Wiley & Sons, pps 98-111 and 472-476.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
138 */
37102
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
139 static unsigned char
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
140 st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
141 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
142 PyInt16 mask;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
143 PyInt16 seg;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
144 unsigned char uval;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
145
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
146 /* The original sox code does this in the calling function, not here */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
147 pcm_val = pcm_val >> 2;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
148
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
149 /* u-law inverts all bits */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
150 /* Get the sign and the magnitude of the value. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
151 if (pcm_val < 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
152 pcm_val = -pcm_val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
153 mask = 0x7F;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
154 } else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
155 mask = 0xFF;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
156 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
157 if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
158 pcm_val += (BIAS >> 2);
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
159
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
160 /* Convert the scaled magnitude to segment number. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
161 seg = search(pcm_val, seg_uend, 8);
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
162
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
163 /*
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
164 * Combine the sign, segment, quantization bits;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
165 * and complement the code word.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
166 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
167 if (seg >= 8) /* out of range, return maximum value. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
168 return (unsigned char) (0x7F ^ mask);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
169 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
170 uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
171 return (uval ^ mask);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
172 }
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
173
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
174 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
37102
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
176 static PyInt16 _st_alaw2linear16[256] = {
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
177 -5504, -5248, -6016, -5760, -4480, -4224, -4992,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
178 -4736, -7552, -7296, -8064, -7808, -6528, -6272,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
179 -7040, -6784, -2752, -2624, -3008, -2880, -2240,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
180 -2112, -2496, -2368, -3776, -3648, -4032, -3904,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
181 -3264, -3136, -3520, -3392, -22016, -20992, -24064,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
182 -23040, -17920, -16896, -19968, -18944, -30208, -29184,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
183 -32256, -31232, -26112, -25088, -28160, -27136, -11008,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
184 -10496, -12032, -11520, -8960, -8448, -9984, -9472,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
185 -15104, -14592, -16128, -15616, -13056, -12544, -14080,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
186 -13568, -344, -328, -376, -360, -280, -264,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
187 -312, -296, -472, -456, -504, -488, -408,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
188 -392, -440, -424, -88, -72, -120, -104,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
189 -24, -8, -56, -40, -216, -200, -248,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
190 -232, -152, -136, -184, -168, -1376, -1312,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
191 -1504, -1440, -1120, -1056, -1248, -1184, -1888,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
192 -1824, -2016, -1952, -1632, -1568, -1760, -1696,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
193 -688, -656, -752, -720, -560, -528, -624,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
194 -592, -944, -912, -1008, -976, -816, -784,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
195 -880, -848, 5504, 5248, 6016, 5760, 4480,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
196 4224, 4992, 4736, 7552, 7296, 8064, 7808,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
197 6528, 6272, 7040, 6784, 2752, 2624, 3008,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
198 2880, 2240, 2112, 2496, 2368, 3776, 3648,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
199 4032, 3904, 3264, 3136, 3520, 3392, 22016,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
200 20992, 24064, 23040, 17920, 16896, 19968, 18944,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
201 30208, 29184, 32256, 31232, 26112, 25088, 28160,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
202 27136, 11008, 10496, 12032, 11520, 8960, 8448,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
203 9984, 9472, 15104, 14592, 16128, 15616, 13056,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
204 12544, 14080, 13568, 344, 328, 376, 360,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
205 280, 264, 312, 296, 472, 456, 504,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
206 488, 408, 392, 440, 424, 88, 72,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
207 120, 104, 24, 8, 56, 40, 216,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
208 200, 248, 232, 152, 136, 184, 168,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
209 1376, 1312, 1504, 1440, 1120, 1056, 1248,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
210 1184, 1888, 1824, 2016, 1952, 1632, 1568,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
211 1760, 1696, 688, 656, 752, 720, 560,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
212 528, 624, 592, 944, 912, 1008, 976,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
213 816, 784, 880, 848
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
214 };
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
216 /*
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
217 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
218 * stored in a unsigned char. This function should only be called with
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
219 * the data shifted such that it only contains information in the lower
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
220 * 13-bits.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
221 *
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
222 * Linear Input Code Compressed Code
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
223 * ------------------------ ---------------
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
224 * 0000000wxyza 000wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
225 * 0000001wxyza 001wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
226 * 000001wxyzab 010wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
227 * 00001wxyzabc 011wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
228 * 0001wxyzabcd 100wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
229 * 001wxyzabcde 101wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
230 * 01wxyzabcdef 110wxyz
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
231 * 1wxyzabcdefg 111wxyz
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
232 *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
233 * For further information see John C. Bellamy's Digital Telephony, 1982,
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
234 * John Wiley & Sons, pps 98-111 and 472-476.
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
235 */
37102
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
236 static unsigned char
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
237 st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
238 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
239 PyInt16 mask;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
240 short seg;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
241 unsigned char aval;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
242
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
243 /* The original sox code does this in the calling function, not here */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
244 pcm_val = pcm_val >> 3;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
245
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
246 /* A-law using even bit inversion */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
247 if (pcm_val >= 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
248 mask = 0xD5; /* sign (7th) bit = 1 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
249 } else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
250 mask = 0x55; /* sign bit = 0 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
251 pcm_val = -pcm_val - 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
252 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
254 /* Convert the scaled magnitude to segment number. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
255 seg = search(pcm_val, seg_aend, 8);
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
256
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
257 /* Combine the sign, segment, and quantization bits. */
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
259 if (seg >= 8) /* out of range, return maximum value. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
260 return (unsigned char) (0x7F ^ mask);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
261 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
262 aval = (unsigned char) seg << SEG_SHIFT;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
263 if (seg < 2)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
264 aval |= (pcm_val >> 1) & QUANT_MASK;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
265 else
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
266 aval |= (pcm_val >> seg) & QUANT_MASK;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
267 return (aval ^ mask);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
268 }
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
269 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 /* End of code taken from sox */
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
272 /* 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
273 static int indexTable[16] = {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
274 -1, -1, -1, -1, 2, 4, 6, 8,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
275 -1, -1, -1, -1, 2, 4, 6, 8,
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
276 };
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
277
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
278 static int stepsizeTable[89] = {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
279 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
280 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
281 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
282 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
283 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
284 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
285 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
286 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
287 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
288 };
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
289
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 #define CHARP(cp, i) ((signed char *)(cp+i))
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 #define SHORTP(cp, i) ((short *)(cp+i))
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6543
diff changeset
292 #define LONGP(cp, i) ((Py_Int32 *)(cp+i))
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
296 static PyObject *AudioopError;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
298 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
299 audioop_getsample(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
301 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
302 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
303 int i;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
305 if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
306 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
307 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
308 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
309 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
310 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
311 if ( i < 0 || i >= len/size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
312 PyErr_SetString(AudioopError, "Index out of range");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
313 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
314 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
315 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
316 else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
317 else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
318 return PyInt_FromLong(val);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
321 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
322 audioop_max(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
324 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
325 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
326 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
327 int max = 0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
329 if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
330 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
331 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
332 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
333 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
334 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
335 for ( i=0; i<len; i+= size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
336 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
337 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
338 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
339 if ( val < 0 ) val = (-val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
340 if ( val > max ) max = val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
341 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
342 return PyInt_FromLong(max);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
345 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
346 audioop_minmax(PyObject *self, PyObject *args)
1866
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
347 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
348 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
349 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
350 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
351 int min = 0x7fffffff, max = -0x7fffffff;
1866
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
352
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
353 if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
354 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
355 if (size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
356 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
357 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
358 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
359 for (i = 0; i < len; i += size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
360 if (size == 1) val = (int) *CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
361 else if (size == 2) val = (int) *SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
362 else if (size == 4) val = (int) *LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
363 if (val > max) max = val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
364 if (val < min) min = val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
365 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
366 return Py_BuildValue("(ii)", min, max);
1866
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
367 }
9c5d333d3300 Added minmax function.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1827
diff changeset
368
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
369 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
370 audioop_avg(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
372 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
373 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
374 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
375 double avg = 0.0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
377 if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
378 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
379 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
380 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
381 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
382 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
383 for ( i=0; i<len; i+= size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
384 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
385 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
386 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
387 avg += val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
388 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
389 if ( len == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
390 val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
391 else
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
392 val = (int)(avg / (double)(len/size));
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
393 return PyInt_FromLong(val);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
396 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
397 audioop_rms(PyObject *self, PyObject *args)
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
398 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
399 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
400 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
401 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
402 double sum_squares = 0.0;
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
403
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
404 if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
405 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
406 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
407 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
408 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
409 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
410 for ( i=0; i<len; i+= size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
411 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
412 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
413 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
414 sum_squares += (double)val*(double)val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
415 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
416 if ( len == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
417 val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
418 else
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
419 val = (int)sqrt(sum_squares / (double)(len/size));
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
420 return PyInt_FromLong(val);
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
421 }
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
422
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13519
diff changeset
423 static double _sum2(short *a, short *b, int len)
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
424 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
425 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
426 double sum = 0.0;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
427
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
428 for( i=0; i<len; i++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
429 sum = sum + (double)a[i]*(double)b[i];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
430 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
431 return sum;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
432 }
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
433
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 ** 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
436 ** 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
437 ** the input signal).
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
438 ** 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
439 **
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
440 ** 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
441 ** 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
442 **
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
443 ** 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
444 ** 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
445 ** 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
446 **
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
447 ** 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
448 ** 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
449 ** 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
450 ** 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
451 **
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
452 ** In the code variables correspond as follows:
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
453 ** cp1 A
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
454 ** cp2 R
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
455 ** len1 N
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
456 ** len2 n
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
457 ** aj_m1 A[j-1]
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
458 ** aj_lm1 A[j+n-1]
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
459 ** sum_ri_2 sum(R[i]^2)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
460 ** sum_aij_2 sum(A[i+j]^2)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
461 ** sum_aij_ri sum(A[i+j]R[i])
1224
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 ** 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
464 ** is completely recalculated each step.
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
465 */
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
466 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
467 audioop_findfit(PyObject *self, PyObject *args)
1223
5ae4a8cf79ef Added lin2lin, findmax, findfactor and fintfit routines
Jack Jansen <jack.jansen@cwi.nl>
parents: 1213
diff changeset
468 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
469 short *cp1, *cp2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
470 int len1, len2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
471 int j, best_j;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
472 double aj_m1, aj_lm1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
473 double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
474
40152
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
475 /* Passing a short** for an 's' argument is correct only
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
476 if the string contents is aligned for interpretation
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
477 as short[]. Due to the definition of PyStringObject,
40152
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
478 this is currently (Python 2.6) the case. */
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
479 if ( !PyArg_ParseTuple(args, "s#s#:findfit",
40152
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
480 (char**)&cp1, &len1, (char**)&cp2, &len2) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
481 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
482 if ( len1 & 1 || len2 & 1 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
483 PyErr_SetString(AudioopError, "Strings should be even-sized");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
484 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
485 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
486 len1 >>= 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
487 len2 >>= 1;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
488
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
489 if ( len1 < len2 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
490 PyErr_SetString(AudioopError, "First sample should be longer");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
491 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
492 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
493 sum_ri_2 = _sum2(cp2, cp2, len2);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
494 sum_aij_2 = _sum2(cp1, cp1, len2);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
495 sum_aij_ri = _sum2(cp1, cp2, len2);
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
496
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
497 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
498
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
499 best_result = result;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
500 best_j = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
501 j = 0;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
502
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
503 for ( j=1; j<=len1-len2; j++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
504 aj_m1 = (double)cp1[j-1];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
505 aj_lm1 = (double)cp1[j+len2-1];
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
506
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
507 sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
508 sum_aij_ri = _sum2(cp1+j, cp2, len2);
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
509
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
510 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
511 / sum_aij_2;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
512
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
513 if ( result < best_result ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
514 best_result = result;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
515 best_j = j;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
516 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
517
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
518 }
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
519
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
520 factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
521
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
522 return Py_BuildValue("(if)", best_j, factor);
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
523 }
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
524
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
525 /*
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
526 ** 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
527 ** 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
528 */
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
529 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
530 audioop_findfactor(PyObject *self, PyObject *args)
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
531 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
532 short *cp1, *cp2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
533 int len1, len2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
534 double sum_ri_2, sum_aij_ri, result;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
535
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
536 if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
40152
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
537 (char**)&cp1, &len1, (char**)&cp2, &len2) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
538 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
539 if ( len1 & 1 || len2 & 1 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
540 PyErr_SetString(AudioopError, "Strings should be even-sized");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
541 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
542 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
543 if ( len1 != len2 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
544 PyErr_SetString(AudioopError, "Samples should be same size");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
545 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
546 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
547 len2 >>= 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
548 sum_ri_2 = _sum2(cp2, cp2, len2);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
549 sum_aij_ri = _sum2(cp1, cp2, len2);
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
550
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
551 result = sum_aij_ri / sum_ri_2;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
552
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
553 return PyFloat_FromDouble(result);
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
554 }
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
555
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
556 /*
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
557 ** 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
558 ** that contains the most energy.
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
559 */
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
560 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
561 audioop_findmax(PyObject *self, PyObject *args)
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
562 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
563 short *cp1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
564 int len1, len2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
565 int j, best_j;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
566 double aj_m1, aj_lm1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
567 double result, best_result;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
568
40152
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
569 if ( !PyArg_ParseTuple(args, "s#i:findmax",
e2df1356349f Fix various minor errors in passing arguments to
Martin v. Löwis <martin@v.loewis.de>
parents: 38474
diff changeset
570 (char**)&cp1, &len1, &len2) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
571 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
572 if ( len1 & 1 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
573 PyErr_SetString(AudioopError, "Strings should be even-sized");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
574 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
575 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
576 len1 >>= 1;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
577
47912
bc943bbfdb1e Issue 3306. Better control for a lenght in findmax() function.
Facundo Batista <facundobatista@gmail.com>
parents: 47587
diff changeset
578 if ( len2 < 0 || len1 < len2 ) {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
579 PyErr_SetString(AudioopError, "Input sample should be longer");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
580 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
581 }
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
582
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
583 result = _sum2(cp1, cp1, len2);
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
584
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
585 best_result = result;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
586 best_j = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
587 j = 0;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
588
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
589 for ( j=1; j<=len1-len2; j++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
590 aj_m1 = (double)cp1[j-1];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
591 aj_lm1 = (double)cp1[j+len2-1];
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
592
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
593 result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
594
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
595 if ( result > best_result ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
596 best_result = result;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
597 best_j = j;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
598 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
599
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
600 }
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
601
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
602 return PyInt_FromLong(best_j);
1223
5ae4a8cf79ef Added lin2lin, findmax, findfactor and fintfit routines
Jack Jansen <jack.jansen@cwi.nl>
parents: 1213
diff changeset
603 }
5ae4a8cf79ef Added lin2lin, findmax, findfactor and fintfit routines
Jack Jansen <jack.jansen@cwi.nl>
parents: 1213
diff changeset
604
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
605 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
606 audioop_avgpp(PyObject *self, PyObject *args)
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
607 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
608 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
609 int len, size, val = 0, prevval = 0, prevextremevalid = 0,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
610 prevextreme = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
611 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
612 double avg = 0.0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
613 int diff, prevdiff, extremediff, nextreme = 0;
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
614
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
615 if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
616 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
617 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
618 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
619 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
620 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
621 /* Compute first delta value ahead. Also automatically makes us
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
622 ** skip the first extreme value
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
623 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
624 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
625 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
626 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
627 if ( size == 1 ) val = (int)*CHARP(cp, size);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
628 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
629 else if ( size == 4 ) val = (int)*LONGP(cp, size);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
630 prevdiff = val - prevval;
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
631
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
632 for ( i=size; i<len; i+= size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
633 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
634 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
635 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
636 diff = val - prevval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
637 if ( diff*prevdiff < 0 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
638 /* Derivative changed sign. Compute difference to last
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
639 ** extreme value and remember.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
640 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
641 if ( prevextremevalid ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
642 extremediff = prevval - prevextreme;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
643 if ( extremediff < 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
644 extremediff = -extremediff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
645 avg += extremediff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
646 nextreme++;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
647 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
648 prevextremevalid = 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
649 prevextreme = prevval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
650 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
651 prevval = val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
652 if ( diff != 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
653 prevdiff = diff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
654 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
655 if ( nextreme == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
656 val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
657 else
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
658 val = (int)(avg / (double)nextreme);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
659 return PyInt_FromLong(val);
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
660 }
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
661
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
662 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
663 audioop_maxpp(PyObject *self, PyObject *args)
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
664 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
665 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
666 int len, size, val = 0, prevval = 0, prevextremevalid = 0,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
667 prevextreme = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
668 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
669 int max = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
670 int diff, prevdiff, extremediff;
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
671
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
672 if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
673 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
674 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
675 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
676 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
677 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
678 /* Compute first delta value ahead. Also automatically makes us
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
679 ** skip the first extreme value
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
680 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
681 if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
682 else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
683 else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
684 if ( size == 1 ) val = (int)*CHARP(cp, size);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
685 else if ( size == 2 ) val = (int)*SHORTP(cp, size);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
686 else if ( size == 4 ) val = (int)*LONGP(cp, size);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
687 prevdiff = val - prevval;
993
7ce1975f11a9 Added rms, maxpp and avgpp methods.
Jack Jansen <jack.jansen@cwi.nl>
parents: 909
diff changeset
688
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
689 for ( i=size; i<len; i+= size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
690 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
691 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
692 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
693 diff = val - prevval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
694 if ( diff*prevdiff < 0 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
695 /* Derivative changed sign. Compute difference to
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
696 ** last extreme value and remember.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
697 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
698 if ( prevextremevalid ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
699 extremediff = prevval - prevextreme;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
700 if ( extremediff < 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
701 extremediff = -extremediff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
702 if ( extremediff > max )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
703 max = extremediff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
704 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
705 prevextremevalid = 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
706 prevextreme = prevval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
707 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
708 prevval = val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
709 if ( diff != 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
710 prevdiff = diff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
711 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
712 return PyInt_FromLong(max);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
714
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
715 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
716 audioop_cross(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
718 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
719 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
720 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
721 int prevval, ncross;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
722
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
723 if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
724 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
725 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
726 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
727 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
728 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
729 ncross = -1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
730 prevval = 17; /* Anything <> 0,1 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
731 for ( i=0; i<len; i+= size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
732 if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
733 else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
734 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
735 val = val & 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
736 if ( val != prevval ) ncross++;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
737 prevval = val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
738 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
739 return PyInt_FromLong(ncross);
877
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
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
742 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
743 audioop_mul(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
744 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
745 signed char *cp, *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
746 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
747 double factor, fval, maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
748 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
749 int i;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
750
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
751 if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
752 return 0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
753
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
754 if ( size == 1 ) maxval = (double) 0x7f;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
755 else if ( size == 2 ) maxval = (double) 0x7fff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
756 else if ( size == 4 ) maxval = (double) 0x7fffffff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
757 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
758 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
759 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
760 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
761
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
762 rv = PyString_FromStringAndSize(NULL, len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
763 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
764 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
765 ncp = (signed char *)PyString_AsString(rv);
877
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
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
768 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
769 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
770 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
771 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
772 fval = (double)val*factor;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
773 if ( fval > maxval ) fval = maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
774 else if ( fval < -maxval ) fval = -maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
775 val = (int)fval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
776 if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
777 else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
778 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
779 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
780 return rv;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
782
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
783 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
784 audioop_tomono(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
786 signed char *cp, *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
787 int len, size, val1 = 0, val2 = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
788 double fac1, fac2, fval, maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
789 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
790 int i;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
792 if ( !PyArg_ParseTuple(args, "s#idd:tomono",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
793 &cp, &len, &size, &fac1, &fac2 ) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
794 return 0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
795
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
796 if ( size == 1 ) maxval = (double) 0x7f;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
797 else if ( size == 2 ) maxval = (double) 0x7fff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
798 else if ( size == 4 ) maxval = (double) 0x7fffffff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
799 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
800 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
801 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
802 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
803
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
804 rv = PyString_FromStringAndSize(NULL, len/2);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
805 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
806 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
807 ncp = (signed char *)PyString_AsString(rv);
877
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
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
810 for ( i=0; i < len; i += size*2 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
811 if ( size == 1 ) val1 = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
812 else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
813 else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
814 if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
815 else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
816 else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
817 fval = (double)val1*fac1 + (double)val2*fac2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
818 if ( fval > maxval ) fval = maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
819 else if ( fval < -maxval ) fval = -maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
820 val1 = (int)fval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
821 if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
822 else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
823 else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
824 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
825 return rv;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
827
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
828 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
829 audioop_tostereo(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
831 signed char *cp, *ncp;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
832 int len, new_len, size, val1, val2, val = 0;
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
833 double fac1, fac2, fval, maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
834 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
835 int i;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
836
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
837 if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
838 &cp, &len, &size, &fac1, &fac2 ) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
839 return 0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
840
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
841 if ( size == 1 ) maxval = (double) 0x7f;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
842 else if ( size == 2 ) maxval = (double) 0x7fff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
843 else if ( size == 4 ) maxval = (double) 0x7fffffff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
844 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
845 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
846 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
847 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
848
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
849 new_len = len*2;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
850 if (new_len < 0) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
851 PyErr_SetString(PyExc_MemoryError,
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
852 "not enough memory for output buffer");
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
853 return 0;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
854 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
855
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
856 rv = PyString_FromStringAndSize(NULL, new_len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
857 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
858 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
859 ncp = (signed char *)PyString_AsString(rv);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
860
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
861
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
862 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
863 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
864 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
865 else if ( size == 4 ) val = (int)*LONGP(cp, i);
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
866
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
867 fval = (double)val*fac1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
868 if ( fval > maxval ) fval = maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
869 else if ( fval < -maxval ) fval = -maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
870 val1 = (int)fval;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
871
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
872 fval = (double)val*fac2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
873 if ( fval > maxval ) fval = maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
874 else if ( fval < -maxval ) fval = -maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
875 val2 = (int)fval;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
876
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
877 if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
878 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
879 else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
880
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
881 if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
882 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
883 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
884 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
885 return rv;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
886 }
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
887
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
888 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
889 audioop_add(PyObject *self, PyObject *args)
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
890 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
891 signed char *cp1, *cp2, *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
892 int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
893 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
894 int i;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
895
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
896 if ( !PyArg_ParseTuple(args, "s#s#i:add",
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
897 &cp1, &len1, &cp2, &len2, &size ) )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
898 return 0;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
899
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
900 if ( len1 != len2 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
901 PyErr_SetString(AudioopError, "Lengths should be the same");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
902 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
903 }
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
904
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
905 if ( size == 1 ) maxval = 0x7f;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
906 else if ( size == 2 ) maxval = 0x7fff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
907 else if ( size == 4 ) maxval = 0x7fffffff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
908 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
909 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
910 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
911 }
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
912
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
913 rv = PyString_FromStringAndSize(NULL, len1);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
914 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
915 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
916 ncp = (signed char *)PyString_AsString(rv);
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
917
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
918 for ( i=0; i < len1; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
919 if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
920 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
921 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
922
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
923 if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
924 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
925 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
926
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
927 newval = val1 + val2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
928 /* truncate in case of overflow */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
929 if (newval > maxval) newval = maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
930 else if (newval < -maxval) newval = -maxval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
931 else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
932 newval = val1 > 0 ? maxval : - maxval;
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
933
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
934 if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
935 else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
936 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
937 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
938 return rv;
877
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
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
941 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
942 audioop_bias(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
944 signed char *cp, *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
945 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
946 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
947 int i;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
948 int bias;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
949
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
950 if ( !PyArg_ParseTuple(args, "s#ii:bias",
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
951 &cp, &len, &size , &bias) )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
952 return 0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
953
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
954 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
955 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
956 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
957 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
958
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
959 rv = PyString_FromStringAndSize(NULL, len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
960 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
961 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
962 ncp = (signed char *)PyString_AsString(rv);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
963
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
964
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
965 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
966 if ( size == 1 ) val = (int)*CHARP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
967 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
968 else if ( size == 4 ) val = (int)*LONGP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
969
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
970 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val+bias);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
971 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
972 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
973 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
974 return rv;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
976
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
977 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
978 audioop_reverse(PyObject *self, PyObject *args)
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
979 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
980 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
981 unsigned char *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
982 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
983 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
984 int i, j;
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
985
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
986 if ( !PyArg_ParseTuple(args, "s#i:reverse",
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
987 &cp, &len, &size) )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
988 return 0;
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
989
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
990 if ( size != 1 && size != 2 && size != 4 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
991 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
992 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
993 }
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
994
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
995 rv = PyString_FromStringAndSize(NULL, len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
996 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
997 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
998 ncp = (unsigned char *)PyString_AsString(rv);
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
999
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1000 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1001 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1002 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1003 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
1004
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1005 j = len - i - size;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1006
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1007 if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1008 else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1009 else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1010 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1011 return rv;
1238
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
1012 }
d426c5d59254 Added audioop.reverse() which reverses an audio sample
Jack Jansen <jack.jansen@cwi.nl>
parents: 1224
diff changeset
1013
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1014 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1015 audioop_lin2lin(PyObject *self, PyObject *args)
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1016 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1017 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1018 unsigned char *ncp;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1019 int len, new_len, size, size2, val = 0;
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1020 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1021 int i, j;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1022
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1023 if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1024 &cp, &len, &size, &size2) )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1025 return 0;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1026
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1027 if ( (size != 1 && size != 2 && size != 4) ||
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1028 (size2 != 1 && size2 != 2 && size2 != 4)) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1029 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1030 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1031 }
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1032
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1033 new_len = (len/size)*size2;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1034 if (new_len < 0) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1035 PyErr_SetString(PyExc_MemoryError,
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1036 "not enough memory for output buffer");
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1037 return 0;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1038 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1039 rv = PyString_FromStringAndSize(NULL, new_len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1040 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1041 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1042 ncp = (unsigned char *)PyString_AsString(rv);
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1043
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1044 for ( i=0, j=0; i < len; i += size, j += size2 ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1045 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1046 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1047 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1048
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1049 if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 8);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1050 else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1051 else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1052 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1053 return rv;
1224
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1054 }
7e27659eb2d9 This time really check in the new routines :-)
Jack Jansen <jack.jansen@cwi.nl>
parents: 1223
diff changeset
1055
5555
989f4ff73429 fixed ratecv to continue working if product of rates is bigger than 32 bits
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1056 static int
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13519
diff changeset
1057 gcd(int a, int b)
5555
989f4ff73429 fixed ratecv to continue working if product of rates is bigger than 32 bits
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1058 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1059 while (b > 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1060 int tmp = a % b;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1061 a = b;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1062 b = tmp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1063 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1064 return a;
5555
989f4ff73429 fixed ratecv to continue working if product of rates is bigger than 32 bits
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1065 }
989f4ff73429 fixed ratecv to continue working if product of rates is bigger than 32 bits
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1066
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1067 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1068 audioop_ratecv(PyObject *self, PyObject *args)
4987
1ded9438fa51 Added Sjoerd's submitted 'ratecv' method. Made corrections for new names.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4879
diff changeset
1069 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1070 char *cp, *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1071 int len, size, nchannels, inrate, outrate, weightA, weightB;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1072 int chan, d, *prev_i, *cur_i, cur_o;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1073 PyObject *state, *samps, *str, *rv = NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1074 int bytes_per_frame;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1075 size_t alloc_size;
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
1076
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1077 weightA = 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1078 weightB = 0;
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1079 if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1080 &nchannels, &inrate, &outrate, &state,
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1081 &weightA, &weightB))
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1082 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1083 if (size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1084 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1085 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1086 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1087 if (nchannels < 1) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1088 PyErr_SetString(AudioopError, "# of channels should be >= 1");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1089 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1090 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1091 bytes_per_frame = size * nchannels;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1092 if (bytes_per_frame / nchannels != size) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1093 /* This overflow test is rigorously correct because
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1094 both multiplicands are >= 1. Use the argument names
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1095 from the docs for the error msg. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1096 PyErr_SetString(PyExc_OverflowError,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1097 "width * nchannels too big for a C int");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1098 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1099 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1100 if (weightA < 1 || weightB < 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1101 PyErr_SetString(AudioopError,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1102 "weightA should be >= 1, weightB should be >= 0");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1103 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1104 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1105 if (len % bytes_per_frame != 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1106 PyErr_SetString(AudioopError, "not a whole number of frames");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1107 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1108 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1109 if (inrate <= 0 || outrate <= 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1110 PyErr_SetString(AudioopError, "sampling rate not > 0");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1111 return NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1112 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1113 /* divide inrate and outrate by their greatest common divisor */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1114 d = gcd(inrate, outrate);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1115 inrate /= d;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1116 outrate /= d;
5555
989f4ff73429 fixed ratecv to continue working if product of rates is bigger than 32 bits
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1117
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1118 alloc_size = sizeof(int) * (unsigned)nchannels;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1119 if (alloc_size < nchannels) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1120 PyErr_SetString(PyExc_MemoryError,
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1121 "not enough memory for output buffer");
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1122 return 0;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1123 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1124 prev_i = (int *) malloc(alloc_size);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1125 cur_i = (int *) malloc(alloc_size);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1126 if (prev_i == NULL || cur_i == NULL) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1127 (void) PyErr_NoMemory();
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1128 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1129 }
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
1130
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1131 len /= bytes_per_frame; /* # of frames */
21218
656c4cf5165f SF bug 482574: audioop.ratecv crashes.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1132
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1133 if (state == Py_None) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1134 d = -outrate;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1135 for (chan = 0; chan < nchannels; chan++)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1136 prev_i[chan] = cur_i[chan] = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1137 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1138 else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1139 if (!PyArg_ParseTuple(state,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1140 "iO!;audioop.ratecv: illegal state argument",
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1141 &d, &PyTuple_Type, &samps))
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1142 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1143 if (PyTuple_Size(samps) != nchannels) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1144 PyErr_SetString(AudioopError,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1145 "illegal state argument");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1146 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1147 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1148 for (chan = 0; chan < nchannels; chan++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1149 if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1150 "ii:ratecv", &prev_i[chan],
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1151 &cur_i[chan]))
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1152 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1153 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1154 }
21218
656c4cf5165f SF bug 482574: audioop.ratecv crashes.
Tim Peters <tim.peters@gmail.com>
parents: 14375
diff changeset
1155
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1156 /* str <- Space for the output buffer. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1157 {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1158 /* There are len input frames, so we need (mathematically)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1159 ceiling(len*outrate/inrate) output frames, and each frame
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1160 requires bytes_per_frame bytes. Computing this
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1161 without spurious overflow is the challenge; we can
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1162 settle for a reasonable upper bound, though. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1163 int ceiling; /* the number of output frames */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1164 int nbytes; /* the number of output bytes needed */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1165 int q = len / inrate;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1166 /* Now len = q * inrate + r exactly (with r = len % inrate),
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1167 and this is less than q * inrate + inrate = (q+1)*inrate.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1168 So a reasonable upper bound on len*outrate/inrate is
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1169 ((q+1)*inrate)*outrate/inrate =
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1170 (q+1)*outrate.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1171 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1172 ceiling = (q+1) * outrate;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1173 nbytes = ceiling * bytes_per_frame;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1174 /* See whether anything overflowed; if not, get the space. */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1175 if (q+1 < 0 ||
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1176 ceiling / outrate != q+1 ||
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1177 nbytes / bytes_per_frame != ceiling)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1178 str = NULL;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1179 else
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1180 str = PyString_FromStringAndSize(NULL, nbytes);
21280
3657befaba4f audioop_ratecv() again: settle for a sloppier upper bound that's less
Tim Peters <tim.peters@gmail.com>
parents: 21239
diff changeset
1181
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1182 if (str == NULL) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1183 PyErr_SetString(PyExc_MemoryError,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1184 "not enough memory for output buffer");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1185 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1186 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1187 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1188 ncp = PyString_AsString(str);
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
1189
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1190 for (;;) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1191 while (d < 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1192 if (len == 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1193 samps = PyTuple_New(nchannels);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1194 if (samps == NULL)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1195 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1196 for (chan = 0; chan < nchannels; chan++)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1197 PyTuple_SetItem(samps, chan,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1198 Py_BuildValue("(ii)",
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1199 prev_i[chan],
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1200 cur_i[chan]));
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1201 if (PyErr_Occurred())
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1202 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1203 /* We have checked before that the length
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1204 * of the string fits into int. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1205 len = (int)(ncp - PyString_AsString(str));
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1206 if (len == 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1207 /*don't want to resize to zero length*/
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1208 rv = PyString_FromStringAndSize("", 0);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1209 Py_DECREF(str);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1210 str = rv;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1211 } else if (_PyString_Resize(&str, len) < 0)
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1212 goto exit;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1213 rv = Py_BuildValue("(O(iO))", str, d, samps);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1214 Py_DECREF(samps);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1215 Py_DECREF(str);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1216 goto exit; /* return rv */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1217 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1218 for (chan = 0; chan < nchannels; chan++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1219 prev_i[chan] = cur_i[chan];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1220 if (size == 1)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1221 cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1222 else if (size == 2)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1223 cur_i[chan] = (int)*SHORTP(cp, 0);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1224 else if (size == 4)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1225 cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1226 cp += size;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1227 /* implements a simple digital filter */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1228 cur_i[chan] =
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1229 (weightA * cur_i[chan] +
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1230 weightB * prev_i[chan]) /
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1231 (weightA + weightB);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1232 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1233 len--;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1234 d += outrate;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1235 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1236 while (d >= 0) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1237 for (chan = 0; chan < nchannels; chan++) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1238 cur_o = (prev_i[chan] * d +
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1239 cur_i[chan] * (outrate - d)) /
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1240 outrate;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1241 if (size == 1)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1242 *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1243 else if (size == 2)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1244 *SHORTP(ncp, 0) = (short)(cur_o);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1245 else if (size == 4)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1246 *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1247 ncp += size;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1248 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1249 d -= inrate;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1250 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1251 }
11256
26fbc80fadf2 Fixed memory leak in ratecv, in response to PR#72. By Sjoerd Mullender.
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1252 exit:
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1253 if (prev_i != NULL)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1254 free(prev_i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1255 if (cur_i != NULL)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1256 free(cur_i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1257 return rv;
4987
1ded9438fa51 Added Sjoerd's submitted 'ratecv' method. Made corrections for new names.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4879
diff changeset
1258 }
5095
11922a2ecacd Changes by Sjoerd (reformatted).
Guido van Rossum <guido@python.org>
parents: 4987
diff changeset
1259
4987
1ded9438fa51 Added Sjoerd's submitted 'ratecv' method. Made corrections for new names.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4879
diff changeset
1260 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1261 audioop_lin2ulaw(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1263 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1264 unsigned char *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1265 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1266 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1267 int i;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1269 if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1270 &cp, &len, &size) )
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1271 return 0 ;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1273 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1274 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1275 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1276 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1278 rv = PyString_FromStringAndSize(NULL, len/size);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1279 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1280 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1281 ncp = (unsigned char *)PyString_AsString(rv);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1283 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1284 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1285 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1286 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1287
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1288 *ncp++ = st_14linear2ulaw(val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1289 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1290 return rv;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1292
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1293 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1294 audioop_ulaw2lin(PyObject *self, PyObject *args)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1295 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1296 unsigned char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1297 unsigned char cval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1298 signed char *ncp;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1299 int len, new_len, size, val;
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1300 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1301 int i;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1303 if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1304 &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1305 return 0;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1306
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1307 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1308 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1309 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1310 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1312 new_len = len*size;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1313 if (new_len < 0) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1314 PyErr_SetString(PyExc_MemoryError,
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1315 "not enough memory for output buffer");
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1316 return 0;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1317 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1318 rv = PyString_FromStringAndSize(NULL, new_len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1319 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1320 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1321 ncp = (signed char *)PyString_AsString(rv);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1323 for ( i=0; i < new_len; i += size ) {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1324 cval = *cp++;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1325 val = st_ulaw2linear16(cval);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1326
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1327 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1328 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1329 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1330 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1331 return rv;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1332 }
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1333
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1334 static PyObject *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1335 audioop_lin2alaw(PyObject *self, PyObject *args)
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1336 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1337 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1338 unsigned char *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1339 int len, size, val = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1340 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1341 int i;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1342
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1343 if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1344 &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1345 return 0;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1346
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1347 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1348 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1349 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1350 }
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1351
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1352 rv = PyString_FromStringAndSize(NULL, len/size);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1353 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1354 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1355 ncp = (unsigned char *)PyString_AsString(rv);
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1356
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1357 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1358 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1359 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1360 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1361
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1362 *ncp++ = st_linear2alaw(val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1363 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1364 return rv;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1365 }
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1366
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1367 static PyObject *
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1368 audioop_alaw2lin(PyObject *self, PyObject *args)
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1369 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1370 unsigned char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1371 unsigned char cval;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1372 signed char *ncp;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1373 int len, new_len, size, val;
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1374 PyObject *rv;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1375 int i;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1376
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1377 if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1378 &cp, &len, &size) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1379 return 0;
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1380
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1381 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1382 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1383 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1384 }
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1385
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1386 new_len = len*size;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1387 if (new_len < 0) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1388 PyErr_SetString(PyExc_MemoryError,
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1389 "not enough memory for output buffer");
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1390 return 0;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1391 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1392 rv = PyString_FromStringAndSize(NULL, new_len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1393 if ( rv == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1394 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1395 ncp = (signed char *)PyString_AsString(rv);
37094
6b516f321d85 SF [ 1231053 ] audioop - alaw encoding/decoding added, code updated
Anthony Baxter <anthonybaxter@gmail.com>
parents: 36861
diff changeset
1396
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1397 for ( i=0; i < new_len; i += size ) {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1398 cval = *cp++;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1399 val = st_alaw2linear16(cval);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1400
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1401 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1402 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1403 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1404 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1405 return rv;
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406 }
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1408 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1409 audioop_lin2adpcm(PyObject *self, PyObject *args)
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1410 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1411 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1412 signed char *ncp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1413 int len, size, val = 0, step, valpred, delta,
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1414 index, sign, vpdiff, diff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1415 PyObject *rv, *state, *str;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1416 int i, outputbuffer = 0, bufferstep;
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1417
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1418 if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1419 &cp, &len, &size, &state) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1420 return 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
1421
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1422
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1423 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1424 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1425 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1426 }
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1427
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1428 str = PyString_FromStringAndSize(NULL, len/(size*2));
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1429 if ( str == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1430 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1431 ncp = (signed char *)PyString_AsString(str);
1223
5ae4a8cf79ef Added lin2lin, findmax, findfactor and fintfit routines
Jack Jansen <jack.jansen@cwi.nl>
parents: 1213
diff changeset
1432
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1433 /* Decode state, should have (value, step) */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1434 if ( state == Py_None ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1435 /* First time, it seems. Set defaults */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1436 valpred = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1437 step = 7;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1438 index = 0;
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1439 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1440 return 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
1441
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1442 step = stepsizeTable[index];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1443 bufferstep = 1;
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1444
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1445 for ( i=0; i < len; i += size ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1446 if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1447 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1448 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 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
1449
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1450 /* Step 1 - compute difference with previous value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1451 diff = val - valpred;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1452 sign = (diff < 0) ? 8 : 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1453 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
1454
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1455 /* Step 2 - Divide and clamp */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1456 /* Note:
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1457 ** This code *approximately* computes:
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1458 ** delta = diff*4/step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1459 ** vpdiff = (delta+0.5)*step/4;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1460 ** but in shift step bits are dropped. The net result of this
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1461 ** is that even if you have fast mul/div hardware you cannot
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1462 ** put it to good use since the fixup would be too expensive.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1463 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1464 delta = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1465 vpdiff = (step >> 3);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1466
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1467 if ( diff >= step ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1468 delta = 4;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1469 diff -= step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1470 vpdiff += step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1471 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1472 step >>= 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1473 if ( diff >= step ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1474 delta |= 2;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1475 diff -= step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1476 vpdiff += step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1477 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1478 step >>= 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1479 if ( diff >= step ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1480 delta |= 1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1481 vpdiff += step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1482 }
1180
1d6cbeb27096 Fixed various bugs in the adpcm routines
Jack Jansen <jack.jansen@cwi.nl>
parents: 993
diff changeset
1483
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1484 /* Step 3 - Update previous value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1485 if ( sign )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1486 valpred -= vpdiff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1487 else
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1488 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
1489
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1490 /* Step 4 - Clamp previous value to 16 bits */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1491 if ( valpred > 32767 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1492 valpred = 32767;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1493 else if ( valpred < -32768 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1494 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
1495
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1496 /* Step 5 - Assemble value, update index and step values */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1497 delta |= sign;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1498
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1499 index += indexTable[delta];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1500 if ( index < 0 ) index = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1501 if ( index > 88 ) index = 88;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1502 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
1503
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1504 /* Step 6 - Output value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1505 if ( bufferstep ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1506 outputbuffer = (delta << 4) & 0xf0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1507 } else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1508 *ncp++ = (delta & 0x0f) | outputbuffer;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1509 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1510 bufferstep = !bufferstep;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1511 }
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1512 rv = Py_BuildValue("(O(ii))", str, valpred, index);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1513 Py_DECREF(str);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1514 return rv;
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1515 }
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1516
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1517 static PyObject *
13519
070e41f2697b ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
1518 audioop_adpcm2lin(PyObject *self, PyObject *args)
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1519 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1520 signed char *cp;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1521 signed char *ncp;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1522 int len, new_len, size, valpred, step, delta, index, sign, vpdiff;
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1523 PyObject *rv, *str, *state;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1524 int i, inputbuffer = 0, bufferstep;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1525
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1526 if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1527 &cp, &len, &size, &state) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1528 return 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
1529
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1530 if ( size != 1 && size != 2 && size != 4) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1531 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1532 return 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1533 }
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1534
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1535 /* Decode state, should have (value, step) */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1536 if ( state == Py_None ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1537 /* First time, it seems. Set defaults */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1538 valpred = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1539 step = 7;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1540 index = 0;
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1541 } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1542 return 0;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1543
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1544 new_len = len*size*2;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1545 if (new_len < 0) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1546 PyErr_SetString(PyExc_MemoryError,
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1547 "not enough memory for output buffer");
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1548 return 0;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1549 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1550 str = PyString_FromStringAndSize(NULL, new_len);
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1551 if ( str == 0 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1552 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
1553 ncp = (signed char *)PyString_AsString(str);
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1554
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1555 step = stepsizeTable[index];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1556 bufferstep = 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
1557
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
1558 for ( i=0; i < new_len; i += size ) {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1559 /* Step 1 - get the delta value and compute next index */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1560 if ( bufferstep ) {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1561 delta = inputbuffer & 0xf;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1562 } else {
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1563 inputbuffer = *cp++;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1564 delta = (inputbuffer >> 4) & 0xf;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1565 }
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1566
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1567 bufferstep = !bufferstep;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1568
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1569 /* Step 2 - Find new index value (for later) */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1570 index += indexTable[delta];
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1571 if ( index < 0 ) index = 0;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1572 if ( index > 88 ) index = 88;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1573
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1574 /* Step 3 - Separate sign and magnitude */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1575 sign = delta & 8;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1576 delta = delta & 7;
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1577
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1578 /* Step 4 - Compute difference and new predicted value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1579 /*
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1580 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1581 ** in adpcm_coder.
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1582 */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1583 vpdiff = step >> 3;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1584 if ( delta & 4 ) vpdiff += step;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1585 if ( delta & 2 ) vpdiff += step>>1;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1586 if ( delta & 1 ) vpdiff += step>>2;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1587
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1588 if ( sign )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1589 valpred -= vpdiff;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1590 else
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1591 valpred += vpdiff;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1592
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1593 /* Step 5 - clamp output value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1594 if ( valpred > 32767 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1595 valpred = 32767;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1596 else if ( valpred < -32768 )
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1597 valpred = -32768;
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1598
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1599 /* Step 6 - Update step value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1600 step = stepsizeTable[index];
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1601
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1602 /* Step 6 - Output value */
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1603 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1604 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1605 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1606 }
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1607
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1608 rv = Py_BuildValue("(O(ii))", str, valpred, index);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1609 Py_DECREF(str);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1610 return rv;
909
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1611 }
4da0b7dae796 Added conversions to/from ADPCM(-4); renamed adpcm to adpcm3 (what it
Guido van Rossum <guido@python.org>
parents: 905
diff changeset
1612
4879
4514023160b4 Renamed, reindented. (was already partially complete)
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
1613 static PyMethodDef audioop_methods[] = {
38474
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1614 { "max", audioop_max, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1615 { "minmax", audioop_minmax, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1616 { "avg", audioop_avg, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1617 { "maxpp", audioop_maxpp, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1618 { "avgpp", audioop_avgpp, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1619 { "rms", audioop_rms, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1620 { "findfit", audioop_findfit, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1621 { "findmax", audioop_findmax, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1622 { "findfactor", audioop_findfactor, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1623 { "cross", audioop_cross, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1624 { "mul", audioop_mul, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1625 { "add", audioop_add, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1626 { "bias", audioop_bias, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1627 { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1628 { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1629 { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1630 { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1631 { "lin2lin", audioop_lin2lin, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1632 { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1633 { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1634 { "tomono", audioop_tomono, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1635 { "tostereo", audioop_tostereo, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1636 { "getsample", audioop_getsample, METH_VARARGS },
bf326581c742 Convert audioop over to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 37102
diff changeset
1637 { "reverse", audioop_reverse, METH_VARARGS },
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1638 { "ratecv", audioop_ratecv, METH_VARARGS },
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1639 { 0, 0 }
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1640 };
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1641
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 21757
diff changeset
1642 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13519
diff changeset
1643 initaudioop(void)
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1644 {
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1645 PyObject *m, *d;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1646 m = Py_InitModule("audioop", audioop_methods);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1647 if (m == NULL)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1648 return;
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1649 d = PyModule_GetDict(m);
37102
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
1650 if (d == NULL)
db986be2c00c Make some functions static, reformat function decl to be consistent and check for error from PyModule_GetDict()
Neal Norwitz <nnorwitz@gmail.com>
parents: 37096
diff changeset
1651 return;
37096
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1652 AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1653 if (AudioopError != NULL)
3dabd9bb496e replace use of int16_t with a (typedef'd) short, to fix Windows buildbots.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37094
diff changeset
1654 PyDict_SetItemString(d,"error",AudioopError);
877
9fb2a868eeee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1655 }