summaryrefslogtreecommitdiff
path: root/Modules/audioop.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/audioop.c')
-rw-r--r--Modules/audioop.c76
1 files changed, 39 insertions, 37 deletions
diff --git a/Modules/audioop.c b/Modules/audioop.c
index 1e131d2476..dcd7788d62 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -5,8 +5,6 @@
#include "Python.h"
-typedef short PyInt16;
-
#if defined(__CHAR_UNSIGNED__)
#if defined(signed)
/* This module currently does not work on systems where only unsigned
@@ -51,13 +49,15 @@ fbound(double val, double minval, double maxval)
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */
-static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
- 0x1FF, 0x3FF, 0x7FF, 0xFFF};
-static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
- 0x3FF, 0x7FF, 0xFFF, 0x1FFF};
+static const int16_t seg_aend[8] = {
+ 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF
+};
+static const int16_t seg_uend[8] = {
+ 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF
+};
-static PyInt16
-search(PyInt16 val, PyInt16 *table, int size)
+static int16_t
+search(int16_t val, const int16_t *table, int size)
{
int i;
@@ -70,7 +70,7 @@ search(PyInt16 val, PyInt16 *table, int size)
#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
-static PyInt16 _st_ulaw2linear16[256] = {
+static const int16_t _st_ulaw2linear16[256] = {
-32124, -31100, -30076, -29052, -28028, -27004, -25980,
-24956, -23932, -22908, -21884, -20860, -19836, -18812,
-17788, -16764, -15996, -15484, -14972, -14460, -13948,
@@ -143,10 +143,10 @@ static PyInt16 _st_ulaw2linear16[256] = {
* John Wiley & Sons, pps 98-111 and 472-476.
*/
static unsigned char
-st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
+st_14linear2ulaw(int16_t pcm_val) /* 2's complement (14-bit range) */
{
- PyInt16 mask;
- PyInt16 seg;
+ int16_t mask;
+ int16_t seg;
unsigned char uval;
/* u-law inverts all bits */
@@ -176,7 +176,7 @@ st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
}
-static PyInt16 _st_alaw2linear16[256] = {
+static const int16_t _st_alaw2linear16[256] = {
-5504, -5248, -6016, -5760, -4480, -4224, -4992,
-4736, -7552, -7296, -8064, -7808, -6528, -6272,
-7040, -6784, -2752, -2624, -3008, -2880, -2240,
@@ -237,10 +237,10 @@ static PyInt16 _st_alaw2linear16[256] = {
* John Wiley & Sons, pps 98-111 and 472-476.
*/
static unsigned char
-st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
+st_linear2alaw(int16_t pcm_val) /* 2's complement (13-bit range) */
{
- PyInt16 mask;
- short seg;
+ int16_t mask;
+ int16_t seg;
unsigned char aval;
/* A-law using even bit inversion */
@@ -270,12 +270,12 @@ st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
/* End of code taken from sox */
/* Intel ADPCM step variation table */
-static int indexTable[16] = {
+static const int indexTable[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
};
-static int stepsizeTable[89] = {
+static const int stepsizeTable[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
@@ -294,8 +294,8 @@ static int stepsizeTable[89] = {
#define GETINT8(cp, i) GETINTX(signed char, (cp), (i))
-#define GETINT16(cp, i) GETINTX(short, (cp), (i))
-#define GETINT32(cp, i) GETINTX(PY_INT32_T, (cp), (i))
+#define GETINT16(cp, i) GETINTX(int16_t, (cp), (i))
+#define GETINT32(cp, i) GETINTX(int32_t, (cp), (i))
#if WORDS_BIGENDIAN
#define GETINT24(cp, i) ( \
@@ -311,8 +311,8 @@ static int stepsizeTable[89] = {
#define SETINT8(cp, i, val) SETINTX(signed char, (cp), (i), (val))
-#define SETINT16(cp, i, val) SETINTX(short, (cp), (i), (val))
-#define SETINT32(cp, i, val) SETINTX(PY_INT32_T, (cp), (i), (val))
+#define SETINT16(cp, i, val) SETINTX(int16_t, (cp), (i), (val))
+#define SETINT32(cp, i, val) SETINTX(int32_t, (cp), (i), (val))
#if WORDS_BIGENDIAN
#define SETINT24(cp, i, val) do { \
@@ -444,7 +444,9 @@ audioop_max_impl(PyObject *module, Py_buffer *fragment, int width)
return NULL;
for (i = 0; i < fragment->len; i += width) {
int val = GETRAWSAMPLE(width, fragment->buf, i);
- if (val < 0) absval = (-val);
+ /* Cast to unsigned before negating. Unsigned overflow is well-
+ defined, but signed overflow is not. */
+ if (val < 0) absval = (unsigned int)-(int64_t)val;
else absval = val;
if (absval > max) max = absval;
}
@@ -540,7 +542,7 @@ audioop_rms_impl(PyObject *module, Py_buffer *fragment, int width)
return PyLong_FromUnsignedLong(res);
}
-static double _sum2(const short *a, const short *b, Py_ssize_t len)
+static double _sum2(const int16_t *a, const int16_t *b, Py_ssize_t len)
{
Py_ssize_t i;
double sum = 0.0;
@@ -598,7 +600,7 @@ audioop_findfit_impl(PyObject *module, Py_buffer *fragment,
Py_buffer *reference)
/*[clinic end generated code: output=5752306d83cbbada input=62c305605e183c9a]*/
{
- const short *cp1, *cp2;
+ const int16_t *cp1, *cp2;
Py_ssize_t len1, len2;
Py_ssize_t j, best_j;
double aj_m1, aj_lm1;
@@ -608,9 +610,9 @@ audioop_findfit_impl(PyObject *module, Py_buffer *fragment,
PyErr_SetString(AudioopError, "Strings should be even-sized");
return NULL;
}
- cp1 = (const short *)fragment->buf;
+ cp1 = (const int16_t *)fragment->buf;
len1 = fragment->len >> 1;
- cp2 = (const short *)reference->buf;
+ cp2 = (const int16_t *)reference->buf;
len2 = reference->len >> 1;
if (len1 < len2) {
@@ -667,7 +669,7 @@ audioop_findfactor_impl(PyObject *module, Py_buffer *fragment,
Py_buffer *reference)
/*[clinic end generated code: output=14ea95652c1afcf8 input=816680301d012b21]*/
{
- const short *cp1, *cp2;
+ const int16_t *cp1, *cp2;
Py_ssize_t len;
double sum_ri_2, sum_aij_ri, result;
@@ -679,8 +681,8 @@ audioop_findfactor_impl(PyObject *module, Py_buffer *fragment,
PyErr_SetString(AudioopError, "Samples should be same size");
return NULL;
}
- cp1 = (const short *)fragment->buf;
- cp2 = (const short *)reference->buf;
+ cp1 = (const int16_t *)fragment->buf;
+ cp2 = (const int16_t *)reference->buf;
len = fragment->len >> 1;
sum_ri_2 = _sum2(cp2, cp2, len);
sum_aij_ri = _sum2(cp1, cp2, len);
@@ -709,7 +711,7 @@ audioop_findmax_impl(PyObject *module, Py_buffer *fragment,
Py_ssize_t length)
/*[clinic end generated code: output=f008128233523040 input=2f304801ed42383c]*/
{
- const short *cp1;
+ const int16_t *cp1;
Py_ssize_t len1;
Py_ssize_t j, best_j;
double aj_m1, aj_lm1;
@@ -719,7 +721,7 @@ audioop_findmax_impl(PyObject *module, Py_buffer *fragment,
PyErr_SetString(AudioopError, "Strings should be even-sized");
return NULL;
}
- cp1 = (const short *)fragment->buf;
+ cp1 = (const int16_t *)fragment->buf;
len1 = fragment->len >> 1;
if (length < 0 || len1 < length) {
@@ -1120,12 +1122,12 @@ audioop_bias_impl(PyObject *module, Py_buffer *fragment, int width, int bias)
if (width == 1)
val = GETINTX(unsigned char, fragment->buf, i);
else if (width == 2)
- val = GETINTX(unsigned short, fragment->buf, i);
+ val = GETINTX(uint16_t, fragment->buf, i);
else if (width == 3)
val = ((unsigned int)GETINT24(fragment->buf, i)) & 0xffffffu;
else {
assert(width == 4);
- val = GETINTX(PY_UINT32_T, fragment->buf, i);
+ val = GETINTX(uint32_t, fragment->buf, i);
}
val += (unsigned int)bias;
@@ -1135,12 +1137,12 @@ audioop_bias_impl(PyObject *module, Py_buffer *fragment, int width, int bias)
if (width == 1)
SETINTX(unsigned char, ncp, i, val);
else if (width == 2)
- SETINTX(unsigned short, ncp, i, val);
+ SETINTX(uint16_t, ncp, i, val);
else if (width == 3)
SETINT24(ncp, i, (int)val);
else {
assert(width == 4);
- SETINTX(PY_UINT32_T, ncp, i, val);
+ SETINTX(uint32_t, ncp, i, val);
}
}
return rv;
@@ -1332,7 +1334,7 @@ audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width,
weightA /= d;
weightB /= d;
- if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) {
+ if ((size_t)nchannels > SIZE_MAX/sizeof(int)) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
return NULL;