summaryrefslogtreecommitdiff
path: root/Modules/audioop.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/audioop.c')
-rw-r--r--Modules/audioop.c1049
1 files changed, 578 insertions, 471 deletions
diff --git a/Modules/audioop.c b/Modules/audioop.c
index 3f047623eb..f8bb18acf9 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -5,18 +5,6 @@
#include "Python.h"
-#if SIZEOF_INT == 4
-typedef int Py_Int32;
-typedef unsigned int Py_UInt32;
-#else
-#if SIZEOF_LONG == 4
-typedef long Py_Int32;
-typedef unsigned long Py_UInt32;
-#else
-#error "No 4-byte integral type"
-#endif
-#endif
-
typedef short PyInt16;
#if defined(__CHAR_UNSIGNED__)
@@ -38,7 +26,7 @@ fbound(double val, double minval, double maxval)
val = maxval;
else if (val < minval + 1)
val = minval;
- return val;
+ return (int)val;
}
@@ -161,9 +149,6 @@ st_14linear2ulaw(PyInt16 pcm_val) /* 2's complement (14-bit range) */
PyInt16 seg;
unsigned char uval;
- /* The original sox code does this in the calling function, not here */
- pcm_val = pcm_val >> 2;
-
/* u-law inverts all bits */
/* Get the sign and the magnitude of the value. */
if (pcm_val < 0) {
@@ -258,9 +243,6 @@ st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
short seg;
unsigned char aval;
- /* The original sox code does this in the calling function, not here */
- pcm_val = pcm_val >> 3;
-
/* A-law using even bit inversion */
if (pcm_val >= 0) {
mask = 0xD5; /* sign (7th) bit = 1 */
@@ -305,10 +287,82 @@ static int stepsizeTable[89] = {
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
-#define CHARP(cp, i) ((signed char *)(cp+i))
-#define SHORTP(cp, i) ((short *)(cp+i))
-#define LONGP(cp, i) ((Py_Int32 *)(cp+i))
+#define GETINTX(T, cp, i) (*(T *)((unsigned char *)(cp) + (i)))
+#define SETINTX(T, cp, i, val) do { \
+ *(T *)((unsigned char *)(cp) + (i)) = (T)(val); \
+ } while (0)
+
+#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))
+
+#if WORDS_BIGENDIAN
+#define GETINT24(cp, i) ( \
+ ((unsigned char *)(cp) + (i))[2] + \
+ (((unsigned char *)(cp) + (i))[1] << 8) + \
+ (((signed char *)(cp) + (i))[0] << 16) )
+#else
+#define GETINT24(cp, i) ( \
+ ((unsigned char *)(cp) + (i))[0] + \
+ (((unsigned char *)(cp) + (i))[1] << 8) + \
+ (((signed char *)(cp) + (i))[2] << 16) )
+#endif
+
+
+#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))
+
+#if WORDS_BIGENDIAN
+#define SETINT24(cp, i, val) do { \
+ ((unsigned char *)(cp) + (i))[2] = (int)(val); \
+ ((unsigned char *)(cp) + (i))[1] = (int)(val) >> 8; \
+ ((signed char *)(cp) + (i))[0] = (int)(val) >> 16; \
+ } while (0)
+#else
+#define SETINT24(cp, i, val) do { \
+ ((unsigned char *)(cp) + (i))[0] = (int)(val); \
+ ((unsigned char *)(cp) + (i))[1] = (int)(val) >> 8; \
+ ((signed char *)(cp) + (i))[2] = (int)(val) >> 16; \
+ } while (0)
+#endif
+
+
+#define GETRAWSAMPLE(size, cp, i) ( \
+ (size == 1) ? (int)GETINT8((cp), (i)) : \
+ (size == 2) ? (int)GETINT16((cp), (i)) : \
+ (size == 3) ? (int)GETINT24((cp), (i)) : \
+ (int)GETINT32((cp), (i)))
+
+#define SETRAWSAMPLE(size, cp, i, val) do { \
+ if (size == 1) \
+ SETINT8((cp), (i), (val)); \
+ else if (size == 2) \
+ SETINT16((cp), (i), (val)); \
+ else if (size == 3) \
+ SETINT24((cp), (i), (val)); \
+ else \
+ SETINT32((cp), (i), (val)); \
+ } while(0)
+
+
+#define GETSAMPLE32(size, cp, i) ( \
+ (size == 1) ? (int)GETINT8((cp), (i)) << 24 : \
+ (size == 2) ? (int)GETINT16((cp), (i)) << 16 : \
+ (size == 3) ? (int)GETINT24((cp), (i)) << 8 : \
+ (int)GETINT32((cp), (i)))
+
+#define SETSAMPLE32(size, cp, i, val) do { \
+ if (size == 1) \
+ SETINT8((cp), (i), (val) >> 24); \
+ else if (size == 2) \
+ SETINT16((cp), (i), (val) >> 16); \
+ else if (size == 3) \
+ SETINT24((cp), (i), (val) >> 8); \
+ else \
+ SETINT32((cp), (i), (val)); \
+ } while(0)
static PyObject *AudioopError;
@@ -316,8 +370,8 @@ static PyObject *AudioopError;
static int
audioop_check_size(int size)
{
- if (size != 1 && size != 2 && size != 4) {
- PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+ if (size < 1 || size > 4) {
+ PyErr_SetString(AudioopError, "Size should be 1, 2, 3 or 4");
return 0;
}
else
@@ -339,123 +393,129 @@ audioop_check_parameters(Py_ssize_t len, int size)
static PyObject *
audioop_getsample(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0;
+ Py_buffer view;
+ Py_ssize_t i;
+ int size;
+ int val;
- if ( !PyArg_ParseTuple(args, "s#in:getsample", &cp, &len, &size, &i) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*in:getsample", &view, &size, &i))
return NULL;
- if ( i < 0 || i >= len/size ) {
+ if (!audioop_check_parameters(view.len, size))
+ goto error;
+ if (i < 0 || i >= view.len/size) {
PyErr_SetString(AudioopError, "Index out of range");
- return 0;
+ goto error;
}
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
- else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
+ val = GETRAWSAMPLE(size, view.buf, i*size);
+ PyBuffer_Release(&view);
return PyLong_FromLong(val);
+
+ error:
+ PyBuffer_Release(&view);
+ return NULL;
}
static PyObject *
audioop_max(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0;
+ Py_buffer view;
+ Py_ssize_t i;
+ int size;
unsigned int absval, max = 0;
- if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:max", &view, &size))
+ return NULL;
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
return NULL;
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ }
+ for (i = 0; i < view.len; i += size) {
+ int val = GETRAWSAMPLE(size, view.buf, i);
if (val < 0) absval = (-val);
else absval = val;
if (absval > max) max = absval;
}
+ PyBuffer_Release(&view);
return PyLong_FromUnsignedLong(max);
}
static PyObject *
audioop_minmax(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0;
+ Py_buffer view;
+ Py_ssize_t i;
+ int size;
/* -1 trick below is needed on Windows to support -0x80000000 without
a warning */
int min = 0x7fffffff, max = -0x7FFFFFFF-1;
- if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
+ if (!PyArg_ParseTuple(args, "y*i:minmax", &view, &size))
return NULL;
- if (!audioop_check_parameters(len, size))
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
return NULL;
- for (i = 0; i < len; i += size) {
- if (size == 1) val = (int) *CHARP(cp, i);
- else if (size == 2) val = (int) *SHORTP(cp, i);
- else if (size == 4) val = (int) *LONGP(cp, i);
+ }
+ for (i = 0; i < view.len; i += size) {
+ int val = GETRAWSAMPLE(size, view.buf, i);
if (val > max) max = val;
if (val < min) min = val;
}
+ PyBuffer_Release(&view);
return Py_BuildValue("(ii)", min, max);
}
static PyObject *
audioop_avg(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0;
- double avg = 0.0;
+ Py_buffer view;
+ Py_ssize_t i;
+ int size, avg;
+ double sum = 0.0;
- if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:avg", &view, &size))
+ return NULL;
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
return NULL;
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- avg += val;
- }
- if ( len == 0 )
- val = 0;
+ }
+ for (i = 0; i < view.len; i += size)
+ sum += GETRAWSAMPLE(size, view.buf, i);
+ if (view.len == 0)
+ avg = 0;
else
- val = (int)floor(avg / (double)(len/size));
- return PyLong_FromLong(val);
+ avg = (int)floor(sum / (double)(view.len/size));
+ PyBuffer_Release(&view);
+ return PyLong_FromLong(avg);
}
static PyObject *
audioop_rms(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0;
+ Py_buffer view;
+ Py_ssize_t i;
+ int size;
unsigned int res;
double sum_squares = 0.0;
- if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:rms", &view, &size))
+ return NULL;
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
return NULL;
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- sum_squares += (double)val*(double)val;
}
- if ( len == 0 )
+ for (i = 0; i < view.len; i += size) {
+ double val = GETRAWSAMPLE(size, view.buf, i);
+ sum_squares += val*val;
+ }
+ if (view.len == 0)
res = 0;
else
- res = (unsigned int)sqrt(sum_squares / (double)(len/size));
+ res = (unsigned int)sqrt(sum_squares / (double)(view.len/size));
+ PyBuffer_Release(&view);
return PyLong_FromUnsignedLong(res);
}
-static double _sum2(short *a, short *b, Py_ssize_t len)
+static double _sum2(const short *a, const short *b, Py_ssize_t len)
{
Py_ssize_t i;
double sum = 0.0;
@@ -501,29 +561,28 @@ static double _sum2(short *a, short *b, Py_ssize_t len)
static PyObject *
audioop_findfit(PyObject *self, PyObject *args)
{
- short *cp1, *cp2;
+ Py_buffer view1;
+ Py_buffer view2;
+ const short *cp1, *cp2;
Py_ssize_t len1, len2;
Py_ssize_t j, best_j;
double aj_m1, aj_lm1;
double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
- /* Passing a short** for an 's' argument is correct only
- if the string contents is aligned for interpretation
- as short[]. Due to the definition of PyBytesObject,
- this is currently (Python 2.6) the case. */
- if ( !PyArg_ParseTuple(args, "s#s#:findfit",
- (char**)&cp1, &len1, (char**)&cp2, &len2) )
- return 0;
- if ( len1 & 1 || len2 & 1 ) {
+ if (!PyArg_ParseTuple(args, "y*y*:findfit", &view1, &view2))
+ return NULL;
+ if (view1.len & 1 || view2.len & 1) {
PyErr_SetString(AudioopError, "Strings should be even-sized");
- return 0;
+ goto error;
}
- len1 >>= 1;
- len2 >>= 1;
+ cp1 = (const short *)view1.buf;
+ len1 = view1.len >> 1;
+ cp2 = (const short *)view2.buf;
+ len2 = view2.len >> 1;
- if ( len1 < len2 ) {
+ if (len1 < len2) {
PyErr_SetString(AudioopError, "First sample should be longer");
- return 0;
+ goto error;
}
sum_ri_2 = _sum2(cp2, cp2, len2);
sum_aij_2 = _sum2(cp1, cp1, len2);
@@ -553,7 +612,14 @@ audioop_findfit(PyObject *self, PyObject *args)
factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
+ PyBuffer_Release(&view1);
+ PyBuffer_Release(&view2);
return Py_BuildValue("(nf)", best_j, factor);
+
+ error:
+ PyBuffer_Release(&view1);
+ PyBuffer_Release(&view2);
+ return NULL;
}
/*
@@ -563,28 +629,38 @@ audioop_findfit(PyObject *self, PyObject *args)
static PyObject *
audioop_findfactor(PyObject *self, PyObject *args)
{
- short *cp1, *cp2;
- Py_ssize_t len1, len2;
+ Py_buffer view1;
+ Py_buffer view2;
+ const short *cp1, *cp2;
+ Py_ssize_t len;
double sum_ri_2, sum_aij_ri, result;
- if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
- (char**)&cp1, &len1, (char**)&cp2, &len2) )
- return 0;
- if ( len1 & 1 || len2 & 1 ) {
+ if (!PyArg_ParseTuple(args, "y*y*:findfactor", &view1, &view2))
+ return NULL;
+ if (view1.len & 1 || view2.len & 1) {
PyErr_SetString(AudioopError, "Strings should be even-sized");
- return 0;
+ goto error;
}
- if ( len1 != len2 ) {
+ if (view1.len != view2.len) {
PyErr_SetString(AudioopError, "Samples should be same size");
- return 0;
+ goto error;
}
- len2 >>= 1;
- sum_ri_2 = _sum2(cp2, cp2, len2);
- sum_aij_ri = _sum2(cp1, cp2, len2);
+ cp1 = (const short *)view1.buf;
+ cp2 = (const short *)view2.buf;
+ len = view1.len >> 1;
+ sum_ri_2 = _sum2(cp2, cp2, len);
+ sum_aij_ri = _sum2(cp1, cp2, len);
result = sum_aij_ri / sum_ri_2;
+ PyBuffer_Release(&view1);
+ PyBuffer_Release(&view2);
return PyFloat_FromDouble(result);
+
+ error:
+ PyBuffer_Release(&view1);
+ PyBuffer_Release(&view2);
+ return NULL;
}
/*
@@ -594,24 +670,25 @@ audioop_findfactor(PyObject *self, PyObject *args)
static PyObject *
audioop_findmax(PyObject *self, PyObject *args)
{
- short *cp1;
+ Py_buffer view;
+ const short *cp1;
Py_ssize_t len1, len2;
Py_ssize_t j, best_j;
double aj_m1, aj_lm1;
double result, best_result;
- if ( !PyArg_ParseTuple(args, "s#n:findmax",
- (char**)&cp1, &len1, &len2) )
- return 0;
- if ( len1 & 1 ) {
+ if (!PyArg_ParseTuple(args, "y*n:findmax", &view, &len2))
+ return NULL;
+ if (view.len & 1) {
PyErr_SetString(AudioopError, "Strings should be even-sized");
- return 0;
+ goto error;
}
- len1 >>= 1;
+ cp1 = (const short *)view.buf;
+ len1 = view.len >> 1;
- if ( len2 < 0 || len1 < len2 ) {
+ if (len2 < 0 || len1 < len2) {
PyErr_SetString(AudioopError, "Input sample should be longer");
- return 0;
+ goto error;
}
result = _sum2(cp1, cp1, len2);
@@ -632,34 +709,39 @@ audioop_findmax(PyObject *self, PyObject *args)
}
+ PyBuffer_Release(&view);
return PyLong_FromSsize_t(best_j);
+
+ error:
+ PyBuffer_Release(&view);
+ return NULL;
}
static PyObject *
audioop_avgpp(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0, prevval = 0, prevextremevalid = 0,
+ Py_buffer view;
+ Py_ssize_t i;
+ int size, prevval, prevextremevalid = 0,
prevextreme = 0;
double sum = 0.0;
unsigned int avg;
int diff, prevdiff, nextreme = 0;
- if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:avgpp", &view, &size))
return NULL;
- if (len <= size)
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
+ return NULL;
+ }
+ if (view.len <= size) {
+ PyBuffer_Release(&view);
return PyLong_FromLong(0);
- if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
- else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
- else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
+ }
+ prevval = GETRAWSAMPLE(size, view.buf, 0);
prevdiff = 17; /* Anything != 0, 1 */
- for ( i=size; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ for (i = size; i < view.len; i += size) {
+ int val = GETRAWSAMPLE(size, view.buf, i);
if (val != prevval) {
diff = val < prevval;
if (prevdiff == !diff) {
@@ -667,7 +749,12 @@ audioop_avgpp(PyObject *self, PyObject *args)
** extreme value and remember.
*/
if (prevextremevalid) {
- sum += fabs((double)prevval - (double)prevextreme);
+ if (prevval < prevextreme)
+ sum += (double)((unsigned int)prevextreme -
+ (unsigned int)prevval);
+ else
+ sum += (double)((unsigned int)prevval -
+ (unsigned int)prevextreme);
nextreme++;
}
prevextremevalid = 1;
@@ -681,33 +768,34 @@ audioop_avgpp(PyObject *self, PyObject *args)
avg = 0;
else
avg = (unsigned int)(sum / (double)nextreme);
+ PyBuffer_Release(&view);
return PyLong_FromUnsignedLong(avg);
}
static PyObject *
audioop_maxpp(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0, prevval = 0, prevextremevalid = 0,
+ Py_buffer view;
+ Py_ssize_t i;
+ int size, prevval, prevextremevalid = 0,
prevextreme = 0;
unsigned int max = 0, extremediff;
int diff, prevdiff;
- if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:maxpp", &view, &size))
+ return NULL;
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
return NULL;
- if (len <= size)
+ }
+ if (view.len <= size) {
+ PyBuffer_Release(&view);
return PyLong_FromLong(0);
- if ( size == 1 ) prevval = (int)*CHARP(cp, 0);
- else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
- else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
+ }
+ prevval = GETRAWSAMPLE(size, view.buf, 0);
prevdiff = 17; /* Anything != 0, 1 */
- for ( i=size; i<len; i+= size) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ for (i = size; i < view.len; i += size) {
+ int val = GETRAWSAMPLE(size, view.buf, i);
if (val != prevval) {
diff = val < prevval;
if (prevdiff == !diff) {
@@ -731,68 +819,67 @@ audioop_maxpp(PyObject *self, PyObject *args)
prevdiff = diff;
}
}
+ PyBuffer_Release(&view);
return PyLong_FromUnsignedLong(max);
}
static PyObject *
audioop_cross(PyObject *self, PyObject *args)
{
- signed char *cp;
- Py_ssize_t len, i;
- int size, val = 0;
+ Py_buffer view;
+ Py_ssize_t i;
+ int size;
int prevval;
Py_ssize_t ncross;
- if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:cross", &view, &size))
return NULL;
+ if (!audioop_check_parameters(view.len, size)) {
+ PyBuffer_Release(&view);
+ return NULL;
+ }
ncross = -1;
prevval = 17; /* Anything <> 0,1 */
- for ( i=0; i<len; i+= size) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) >> 7;
- else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
- val = val & 1;
- if ( val != prevval ) ncross++;
+ for (i = 0; i < view.len; i += size) {
+ int val = GETRAWSAMPLE(size, view.buf, i) < 0;
+ if (val != prevval) ncross++;
prevval = val;
}
+ PyBuffer_Release(&view);
return PyLong_FromSsize_t(ncross);
}
static PyObject *
audioop_mul(PyObject *self, PyObject *args)
{
- signed char *cp, *ncp;
- Py_ssize_t len, i;
- int size, val = 0;
- double factor, fval, maxval, minval;
- PyObject *rv;
+ Py_buffer view;
+ signed char *ncp;
+ Py_ssize_t i;
+ int size;
+ double factor, maxval, minval;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*id:mul", &view, &size, &factor))
return NULL;
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
maxval = (double) maxvals[size];
minval = (double) minvals[size];
- rv = PyBytes_FromStringAndSize(NULL, len);
- if ( rv == 0 )
- return 0;
+ rv = PyBytes_FromStringAndSize(NULL, view.len);
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
- fval = (double)val*factor;
- val = (int)floor(fbound(fval, minval, maxval));
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)val;
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
+ for (i = 0; i < view.len; i += size) {
+ double val = GETRAWSAMPLE(size, view.buf, i);
+ val *= factor;
+ val = floor(fbound(val, minval, maxval));
+ SETRAWSAMPLE(size, ncp, i, (int)val);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
@@ -802,49 +889,38 @@ audioop_tomono(PyObject *self, PyObject *args)
Py_buffer pcp;
signed char *cp, *ncp;
Py_ssize_t len, i;
- int size, val1 = 0, val2 = 0;
- double fac1, fac2, fval, maxval, minval;
- PyObject *rv;
+ int size;
+ double fac1, fac2, maxval, minval;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s*idd:tomono",
- &pcp, &size, &fac1, &fac2 ) )
- return 0;
+ if (!PyArg_ParseTuple(args, "y*idd:tomono",
+ &pcp, &size, &fac1, &fac2))
+ return NULL;
cp = pcp.buf;
len = pcp.len;
- if (!audioop_check_parameters(len, size)) {
- PyBuffer_Release(&pcp);
- return NULL;
- }
+ if (!audioop_check_parameters(len, size))
+ goto exit;
if (((len / size) & 1) != 0) {
PyErr_SetString(AudioopError, "not a whole number of frames");
- PyBuffer_Release(&pcp);
- return NULL;
+ goto exit;
}
maxval = (double) maxvals[size];
minval = (double) minvals[size];
rv = PyBytes_FromStringAndSize(NULL, len/2);
- if ( rv == 0 ) {
- PyBuffer_Release(&pcp);
- return 0;
- }
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len; i += size*2 ) {
- if ( size == 1 ) val1 = (int)*CHARP(cp, i);
- else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
- if ( size == 1 ) val2 = (int)*CHARP(cp, i+1);
- else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
- else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
- fval = (double)val1*fac1 + (double)val2*fac2;
- val1 = (int)floor(fbound(fval, minval, maxval));
- if ( size == 1 ) *CHARP(ncp, i/2) = (signed char)val1;
- else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
- else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
+ for (i = 0; i < len; i += size*2) {
+ double val1 = GETRAWSAMPLE(size, cp, i);
+ double val2 = GETRAWSAMPLE(size, cp, i + size);
+ double val = val1*fac1 + val2*fac2;
+ val = floor(fbound(val, minval, maxval));
+ SETRAWSAMPLE(size, ncp, i/2, val);
}
+ exit:
PyBuffer_Release(&pcp);
return rv;
}
@@ -852,89 +928,76 @@ audioop_tomono(PyObject *self, PyObject *args)
static PyObject *
audioop_tostereo(PyObject *self, PyObject *args)
{
- signed char *cp, *ncp;
- Py_ssize_t len, i;
- int size, val1, val2, val = 0;
- double fac1, fac2, fval, maxval, minval;
- PyObject *rv;
+ Py_buffer view;
+ signed char *ncp;
+ Py_ssize_t i;
+ int size;
+ double fac1, fac2, maxval, minval;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
- &cp, &len, &size, &fac1, &fac2 ) )
- return 0;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*idd:tostereo",
+ &view, &size, &fac1, &fac2))
return NULL;
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
maxval = (double) maxvals[size];
minval = (double) minvals[size];
- if (len > PY_SSIZE_T_MAX/2) {
+ if (view.len > PY_SSIZE_T_MAX/2) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
- return 0;
+ goto exit;
}
- rv = PyBytes_FromStringAndSize(NULL, len*2);
- if ( rv == 0 )
- return 0;
+ rv = PyBytes_FromStringAndSize(NULL, view.len*2);
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
-
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = (int)*CHARP(cp, i);
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
-
- fval = (double)val*fac1;
- val1 = (int)floor(fbound(fval, minval, maxval));
-
- fval = (double)val*fac2;
- val2 = (int)floor(fbound(fval, minval, maxval));
-
- if ( size == 1 ) *CHARP(ncp, i*2) = (signed char)val1;
- else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
- else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
-
- if ( size == 1 ) *CHARP(ncp, i*2+1) = (signed char)val2;
- else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
- else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
+ for (i = 0; i < view.len; i += size) {
+ double val = GETRAWSAMPLE(size, view.buf, i);
+ int val1 = (int)floor(fbound(val*fac1, minval, maxval));
+ int val2 = (int)floor(fbound(val*fac2, minval, maxval));
+ SETRAWSAMPLE(size, ncp, i*2, val1);
+ SETRAWSAMPLE(size, ncp, i*2 + size, val2);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_add(PyObject *self, PyObject *args)
{
- signed char *cp1, *cp2, *ncp;
- Py_ssize_t len1, len2, i;
- int size, val1 = 0, val2 = 0, minval, maxval, newval;
- PyObject *rv;
+ Py_buffer view1;
+ Py_buffer view2;
+ signed char *ncp;
+ Py_ssize_t i;
+ int size, minval, maxval, newval;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s#s#i:add",
- &cp1, &len1, &cp2, &len2, &size ) )
- return 0;
- if (!audioop_check_parameters(len1, size))
+ if (!PyArg_ParseTuple(args, "y*y*i:add",
+ &view1, &view2, &size))
return NULL;
- if ( len1 != len2 ) {
+ if (!audioop_check_parameters(view1.len, size))
+ goto exit;
+ if (view1.len != view2.len) {
PyErr_SetString(AudioopError, "Lengths should be the same");
- return 0;
+ goto exit;
}
maxval = maxvals[size];
minval = minvals[size];
- rv = PyBytes_FromStringAndSize(NULL, len1);
- if ( rv == 0 )
- return 0;
+ rv = PyBytes_FromStringAndSize(NULL, view1.len);
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
- for ( i=0; i < len1; i += size ) {
- if ( size == 1 ) val1 = (int)*CHARP(cp1, i);
- else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
- else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
-
- if ( size == 1 ) val2 = (int)*CHARP(cp2, i);
- else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
- else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
+ for (i = 0; i < view1.len; i += size) {
+ int val1 = GETRAWSAMPLE(size, view1.buf, i);
+ int val2 = GETRAWSAMPLE(size, view2.buf, i);
if (size < 4) {
newval = val1 + val2;
@@ -950,124 +1013,165 @@ audioop_add(PyObject *self, PyObject *args)
newval = (int)floor(fbound(fval, minval, maxval));
}
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)newval;
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
+ SETRAWSAMPLE(size, ncp, i, newval);
}
+ exit:
+ PyBuffer_Release(&view1);
+ PyBuffer_Release(&view2);
return rv;
}
static PyObject *
audioop_bias(PyObject *self, PyObject *args)
{
- signed char *cp, *ncp;
- Py_ssize_t len, i;
+ Py_buffer view;
+ signed char *ncp;
+ Py_ssize_t i;
int size, bias;
unsigned int val = 0, mask;
- PyObject *rv;
-
- if ( !PyArg_ParseTuple(args, "s#ii:bias",
- &cp, &len, &size , &bias) )
- return 0;
+ PyObject *rv = NULL;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*ii:bias",
+ &view, &size, &bias))
return NULL;
- rv = PyBytes_FromStringAndSize(NULL, len);
- if ( rv == 0 )
- return 0;
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
+
+ rv = PyBytes_FromStringAndSize(NULL, view.len);
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
mask = masks[size];
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = (unsigned int)(unsigned char)*CHARP(cp, i);
- else if ( size == 2 ) val = (unsigned int)(unsigned short)*SHORTP(cp, i);
- else if ( size == 4 ) val = (unsigned int)(Py_UInt32)*LONGP(cp, i);
+ for (i = 0; i < view.len; i += size) {
+ if (size == 1)
+ val = GETINTX(unsigned char, view.buf, i);
+ else if (size == 2)
+ val = GETINTX(unsigned short, view.buf, i);
+ else if (size == 3)
+ val = ((unsigned int)GETINT24(view.buf, i)) & 0xffffffu;
+ else {
+ assert(size == 4);
+ val = GETINTX(PY_UINT32_T, view.buf, i);
+ }
val += (unsigned int)bias;
/* wrap around in case of overflow */
val &= mask;
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(unsigned char)val;
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(unsigned short)val;
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(Py_UInt32)val;
+ if (size == 1)
+ SETINTX(unsigned char, ncp, i, val);
+ else if (size == 2)
+ SETINTX(unsigned short, ncp, i, val);
+ else if (size == 3)
+ SETINT24(ncp, i, (int)val);
+ else {
+ assert(size == 4);
+ SETINTX(PY_UINT32_T, ncp, i, val);
+ }
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_reverse(PyObject *self, PyObject *args)
{
- signed char *cp;
+ Py_buffer view;
unsigned char *ncp;
- Py_ssize_t len, i, j;
- int size, val = 0;
- PyObject *rv;
-
- if ( !PyArg_ParseTuple(args, "s#i:reverse",
- &cp, &len, &size) )
- return 0;
+ Py_ssize_t i;
+ int size;
+ PyObject *rv = NULL;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:reverse",
+ &view, &size))
return NULL;
- rv = PyBytes_FromStringAndSize(NULL, len);
- if ( rv == 0 )
- return 0;
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
+
+ rv = PyBytes_FromStringAndSize(NULL, view.len);
+ if (rv == NULL)
+ goto exit;
ncp = (unsigned char *)PyBytes_AsString(rv);
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 24;
- else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) << 16;
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
+ for (i = 0; i < view.len; i += size) {
+ int val = GETRAWSAMPLE(size, view.buf, i);
+ SETRAWSAMPLE(size, ncp, view.len - i - size, val);
+ }
+ exit:
+ PyBuffer_Release(&view);
+ return rv;
+}
- j = len - i - size;
+static PyObject *
+audioop_byteswap(PyObject *self, PyObject *args)
+{
+ Py_buffer view;
+ unsigned char *ncp;
+ Py_ssize_t i;
+ int size;
+ PyObject *rv = NULL;
- if ( size == 1 ) *CHARP(ncp, j) = (signed char)(val >> 24);
- else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val >> 16);
- else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)val;
+ if (!PyArg_ParseTuple(args, "y*i:swapbytes",
+ &view, &size))
+ return NULL;
+
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
+
+ rv = PyBytes_FromStringAndSize(NULL, view.len);
+ if (rv == NULL)
+ goto exit;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
+
+ for (i = 0; i < view.len; i += size) {
+ int j;
+ for (j = 0; j < size; j++)
+ ncp[i + size - 1 - j] = ((unsigned char *)view.buf)[i + j];
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_lin2lin(PyObject *self, PyObject *args)
{
- signed char *cp;
+ Py_buffer view;
unsigned char *ncp;
- Py_ssize_t len, i, j;
- int size, size2, val = 0;
- PyObject *rv;
+ Py_ssize_t i, j;
+ int size, size2;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
- &cp, &len, &size, &size2) )
- return 0;
-
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*ii:lin2lin",
+ &view, &size, &size2))
return NULL;
+
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
if (!audioop_check_size(size2))
- return NULL;
+ goto exit;
- if (len/size > PY_SSIZE_T_MAX/size2) {
+ if (view.len/size > PY_SSIZE_T_MAX/size2) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
- return 0;
+ goto exit;
}
- rv = PyBytes_FromStringAndSize(NULL, (len/size)*size2);
- if ( rv == 0 )
- return 0;
+ rv = PyBytes_FromStringAndSize(NULL, (view.len/size)*size2);
+ if (rv == NULL)
+ goto exit;
ncp = (unsigned char *)PyBytes_AsString(rv);
- for ( i=0, j=0; i < len; i += size, j += size2 ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 24;
- else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) << 16;
- else if ( size == 4 ) val = (int)*LONGP(cp, i);
-
- if ( size2 == 1 ) *CHARP(ncp, j) = (signed char)(val >> 24);
- else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val >> 16);
- else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)val;
+ for (i = j = 0; i < view.len; i += size, j += size2) {
+ int val = GETSAMPLE32(size, view.buf, i);
+ SETSAMPLE32(size2, ncp, j, val);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
@@ -1085,6 +1189,7 @@ gcd(int a, int b)
static PyObject *
audioop_ratecv(PyObject *self, PyObject *args)
{
+ Py_buffer view;
char *cp, *ncp;
Py_ssize_t len;
int size, nchannels, inrate, outrate, weightA, weightB;
@@ -1094,15 +1199,15 @@ audioop_ratecv(PyObject *self, PyObject *args)
weightA = 1;
weightB = 0;
- if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
+ if (!PyArg_ParseTuple(args, "y*iiiiO|ii:ratecv", &view, &size,
&nchannels, &inrate, &outrate, &state,
&weightA, &weightB))
return NULL;
if (!audioop_check_size(size))
- return NULL;
+ goto exit2;
if (nchannels < 1) {
PyErr_SetString(AudioopError, "# of channels should be >= 1");
- return NULL;
+ goto exit2;
}
if (size > INT_MAX / nchannels) {
/* This overflow test is rigorously correct because
@@ -1110,21 +1215,21 @@ audioop_ratecv(PyObject *self, PyObject *args)
from the docs for the error msg. */
PyErr_SetString(PyExc_OverflowError,
"width * nchannels too big for a C int");
- return NULL;
+ goto exit2;
}
bytes_per_frame = size * nchannels;
if (weightA < 1 || weightB < 0) {
PyErr_SetString(AudioopError,
"weightA should be >= 1, weightB should be >= 0");
- return NULL;
+ goto exit2;
}
- if (len % bytes_per_frame != 0) {
+ if (view.len % bytes_per_frame != 0) {
PyErr_SetString(AudioopError, "not a whole number of frames");
- return NULL;
+ goto exit2;
}
if (inrate <= 0 || outrate <= 0) {
PyErr_SetString(AudioopError, "sampling rate not > 0");
- return NULL;
+ goto exit2;
}
/* divide inrate and outrate by their greatest common divisor */
d = gcd(inrate, outrate);
@@ -1138,16 +1243,16 @@ audioop_ratecv(PyObject *self, PyObject *args)
if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
- return 0;
+ goto exit2;
}
- prev_i = (int *) malloc(nchannels * sizeof(int));
- cur_i = (int *) malloc(nchannels * sizeof(int));
+ prev_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
+ cur_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
if (prev_i == NULL || cur_i == NULL) {
(void) PyErr_NoMemory();
goto exit;
}
- len /= bytes_per_frame; /* # of frames */
+ len = view.len / bytes_per_frame; /* # of frames */
if (state == Py_None) {
d = -outrate;
@@ -1197,6 +1302,7 @@ audioop_ratecv(PyObject *self, PyObject *args)
goto exit;
}
ncp = PyBytes_AsString(str);
+ cp = view.buf;
for (;;) {
while (d < 0) {
@@ -1227,12 +1333,7 @@ audioop_ratecv(PyObject *self, PyObject *args)
}
for (chan = 0; chan < nchannels; chan++) {
prev_i[chan] = cur_i[chan];
- if (size == 1)
- cur_i[chan] = ((int)*CHARP(cp, 0)) << 24;
- else if (size == 2)
- cur_i[chan] = ((int)*SHORTP(cp, 0)) << 16;
- else if (size == 4)
- cur_i[chan] = (int)*LONGP(cp, 0);
+ cur_i[chan] = GETSAMPLE32(size, cp, 0);
cp += size;
/* implements a simple digital filter */
cur_i[chan] = (int)(
@@ -1248,184 +1349,175 @@ audioop_ratecv(PyObject *self, PyObject *args)
cur_o = (int)(((double)prev_i[chan] * (double)d +
(double)cur_i[chan] * (double)(outrate - d)) /
(double)outrate);
- if (size == 1)
- *CHARP(ncp, 0) = (signed char)(cur_o >> 24);
- else if (size == 2)
- *SHORTP(ncp, 0) = (short)(cur_o >> 16);
- else if (size == 4)
- *LONGP(ncp, 0) = (Py_Int32)(cur_o);
+ SETSAMPLE32(size, ncp, 0, cur_o);
ncp += size;
}
d -= inrate;
}
}
exit:
- if (prev_i != NULL)
- free(prev_i);
- if (cur_i != NULL)
- free(cur_i);
+ PyMem_Free(prev_i);
+ PyMem_Free(cur_i);
+ exit2:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_lin2ulaw(PyObject *self, PyObject *args)
{
- signed char *cp;
+ Py_buffer view;
unsigned char *ncp;
- Py_ssize_t len, i;
- int size, val = 0;
- PyObject *rv;
-
- if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
- &cp, &len, &size) )
- return 0 ;
+ Py_ssize_t i;
+ int size;
+ PyObject *rv = NULL;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:lin2ulaw",
+ &view, &size))
return NULL;
- rv = PyBytes_FromStringAndSize(NULL, len/size);
- if ( rv == 0 )
- return 0;
- ncp = (unsigned char *)PyBytes_AsString(rv);
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+ rv = PyBytes_FromStringAndSize(NULL, view.len/size);
+ if (rv == NULL)
+ goto exit;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
- *ncp++ = st_14linear2ulaw(val);
+ for (i = 0; i < view.len; i += size) {
+ int val = GETSAMPLE32(size, view.buf, i);
+ *ncp++ = st_14linear2ulaw(val >> 18);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_ulaw2lin(PyObject *self, PyObject *args)
{
+ Py_buffer view;
unsigned char *cp;
- unsigned char cval;
signed char *ncp;
- Py_ssize_t len, i;
- int size, val;
- PyObject *rv;
+ Py_ssize_t i;
+ int size;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
- &cp, &len, &size) )
- return 0;
+ if (!PyArg_ParseTuple(args, "y*i:ulaw2lin",
+ &view, &size))
+ return NULL;
if (!audioop_check_size(size))
- return NULL;
+ goto exit;
- if (len > PY_SSIZE_T_MAX/size) {
+ if (view.len > PY_SSIZE_T_MAX/size) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
- return 0;
+ goto exit;
}
- rv = PyBytes_FromStringAndSize(NULL, len*size);
- if ( rv == 0 )
- return 0;
+ rv = PyBytes_FromStringAndSize(NULL, view.len*size);
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
- for ( i=0; i < len*size; i += size ) {
- cval = *cp++;
- val = st_ulaw2linear16(cval);
-
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
+ cp = view.buf;
+ for (i = 0; i < view.len*size; i += size) {
+ int val = st_ulaw2linear16(*cp++) << 16;
+ SETSAMPLE32(size, ncp, i, val);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_lin2alaw(PyObject *self, PyObject *args)
{
- signed char *cp;
+ Py_buffer view;
unsigned char *ncp;
- Py_ssize_t len, i;
- int size, val = 0;
- PyObject *rv;
-
- if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
- &cp, &len, &size) )
- return 0;
+ Py_ssize_t i;
+ int size;
+ PyObject *rv = NULL;
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*i:lin2alaw",
+ &view, &size))
return NULL;
- rv = PyBytes_FromStringAndSize(NULL, len/size);
- if ( rv == 0 )
- return 0;
- ncp = (unsigned char *)PyBytes_AsString(rv);
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+ rv = PyBytes_FromStringAndSize(NULL, view.len/size);
+ if (rv == NULL)
+ goto exit;
+ ncp = (unsigned char *)PyBytes_AsString(rv);
- *ncp++ = st_linear2alaw(val);
+ for (i = 0; i < view.len; i += size) {
+ int val = GETSAMPLE32(size, view.buf, i);
+ *ncp++ = st_linear2alaw(val >> 19);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_alaw2lin(PyObject *self, PyObject *args)
{
+ Py_buffer view;
unsigned char *cp;
- unsigned char cval;
signed char *ncp;
- Py_ssize_t len, i;
+ Py_ssize_t i;
int size, val;
- PyObject *rv;
+ PyObject *rv = NULL;
- if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
- &cp, &len, &size) )
- return 0;
+ if (!PyArg_ParseTuple(args, "y*i:alaw2lin",
+ &view, &size))
+ return NULL;
if (!audioop_check_size(size))
- return NULL;
+ goto exit;
- if (len > PY_SSIZE_T_MAX/size) {
+ if (view.len > PY_SSIZE_T_MAX/size) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
- return 0;
+ goto exit;
}
- rv = PyBytes_FromStringAndSize(NULL, len*size);
- if ( rv == 0 )
- return 0;
+ rv = PyBytes_FromStringAndSize(NULL, view.len*size);
+ if (rv == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(rv);
+ cp = view.buf;
- for ( i=0; i < len*size; i += size ) {
- cval = *cp++;
- val = st_alaw2linear16(cval);
-
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(val >> 8);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
+ for (i = 0; i < view.len*size; i += size) {
+ val = st_alaw2linear16(*cp++) << 16;
+ SETSAMPLE32(size, ncp, i, val);
}
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_lin2adpcm(PyObject *self, PyObject *args)
{
- signed char *cp;
+ Py_buffer view;
signed char *ncp;
- Py_ssize_t len, i;
- int size, val = 0, step, valpred, delta,
+ Py_ssize_t i;
+ int size, step, valpred, delta,
index, sign, vpdiff, diff;
- PyObject *rv, *state, *str;
+ PyObject *rv = NULL, *state, *str = NULL;
int outputbuffer = 0, bufferstep;
- if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
- &cp, &len, &size, &state) )
- return 0;
-
- if (!audioop_check_parameters(len, size))
+ if (!PyArg_ParseTuple(args, "y*iO:lin2adpcm",
+ &view, &size, &state))
return NULL;
- str = PyBytes_FromStringAndSize(NULL, len/(size*2));
- if ( str == 0 )
- return 0;
+ if (!audioop_check_parameters(view.len, size))
+ goto exit;
+
+ str = PyBytes_FromStringAndSize(NULL, view.len/(size*2));
+ if (str == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(str);
/* Decode state, should have (value, step) */
@@ -1433,21 +1525,27 @@ audioop_lin2adpcm(PyObject *self, PyObject *args)
/* First time, it seems. Set defaults */
valpred = 0;
index = 0;
- } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
- return 0;
+ } else if (!PyTuple_Check(state)) {
+ PyErr_SetString(PyExc_TypeError, "state must be a tuple or None");
+ goto exit;
+ } else if (!PyArg_ParseTuple(state, "ii", &valpred, &index))
+ goto exit;
step = stepsizeTable[index];
bufferstep = 1;
- for ( i=0; i < len; i += size ) {
- if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
- else if ( size == 2 ) val = (int)*SHORTP(cp, i);
- else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
+ for (i = 0; i < view.len; i += size) {
+ int val = GETSAMPLE32(size, view.buf, i) >> 16;
/* Step 1 - compute difference with previous value */
- diff = val - valpred;
- sign = (diff < 0) ? 8 : 0;
- if ( sign ) diff = (-diff);
+ if (val < valpred) {
+ diff = valpred - val;
+ sign = 8;
+ }
+ else {
+ diff = val - valpred;
+ sign = 0;
+ }
/* Step 2 - Divide and clamp */
/* Note:
@@ -1507,49 +1605,57 @@ audioop_lin2adpcm(PyObject *self, PyObject *args)
bufferstep = !bufferstep;
}
rv = Py_BuildValue("(O(ii))", str, valpred, index);
- Py_DECREF(str);
+ exit:
+ Py_XDECREF(str);
+ PyBuffer_Release(&view);
return rv;
}
static PyObject *
audioop_adpcm2lin(PyObject *self, PyObject *args)
{
+ Py_buffer view;
signed char *cp;
signed char *ncp;
- Py_ssize_t len, i;
+ Py_ssize_t i, outlen;
int size, valpred, step, delta, index, sign, vpdiff;
- PyObject *rv, *str, *state;
+ PyObject *rv = NULL, *str, *state;
int inputbuffer = 0, bufferstep;
- if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
- &cp, &len, &size, &state) )
- return 0;
+ if (!PyArg_ParseTuple(args, "y*iO:adpcm2lin",
+ &view, &size, &state))
+ return NULL;
if (!audioop_check_size(size))
- return NULL;
+ goto exit;
/* Decode state, should have (value, step) */
if ( state == Py_None ) {
/* First time, it seems. Set defaults */
valpred = 0;
index = 0;
- } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
- return 0;
+ } else if (!PyTuple_Check(state)) {
+ PyErr_SetString(PyExc_TypeError, "state must be a tuple or None");
+ goto exit;
+ } else if (!PyArg_ParseTuple(state, "ii", &valpred, &index))
+ goto exit;
- if (len > (PY_SSIZE_T_MAX/2)/size) {
+ if (view.len > (PY_SSIZE_T_MAX/2)/size) {
PyErr_SetString(PyExc_MemoryError,
"not enough memory for output buffer");
- return 0;
+ goto exit;
}
- str = PyBytes_FromStringAndSize(NULL, len*size*2);
- if ( str == 0 )
- return 0;
+ outlen = view.len*size*2;
+ str = PyBytes_FromStringAndSize(NULL, outlen);
+ if (str == NULL)
+ goto exit;
ncp = (signed char *)PyBytes_AsString(str);
+ cp = view.buf;
step = stepsizeTable[index];
bufferstep = 0;
- for ( i=0; i < len*size*2; i += size ) {
+ for (i = 0; i < outlen; i += size) {
/* Step 1 - get the delta value and compute next index */
if ( bufferstep ) {
delta = inputbuffer & 0xf;
@@ -1594,13 +1700,13 @@ audioop_adpcm2lin(PyObject *self, PyObject *args)
step = stepsizeTable[index];
/* Step 6 - Output value */
- if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
- else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
- else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
+ SETSAMPLE32(size, ncp, i, valpred << 16);
}
rv = Py_BuildValue("(O(ii))", str, valpred, index);
Py_DECREF(str);
+ exit:
+ PyBuffer_Release(&view);
return rv;
}
@@ -1629,6 +1735,7 @@ static PyMethodDef audioop_methods[] = {
{ "tostereo", audioop_tostereo, METH_VARARGS },
{ "getsample", audioop_getsample, METH_VARARGS },
{ "reverse", audioop_reverse, METH_VARARGS },
+ { "byteswap", audioop_byteswap, METH_VARARGS },
{ "ratecv", audioop_ratecv, METH_VARARGS },
{ 0, 0 }
};