Renamed, reindented. (was already partially complete)

This commit is contained in:
Roger E. Masse 1997-01-03 19:26:27 +00:00
parent 653071731a
commit eaa6e1102f
1 changed files with 913 additions and 907 deletions

View File

@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* audioopmodule - Module to detect peak values in arrays */
#include "allobjects.h"
#include "modsupport.h"
#include "Python.h"
#if defined(__CHAR_UNSIGNED__)
#if defined(signed)
@ -49,7 +48,9 @@ PERFORMANCE OF THIS SOFTWARE.
#define MINLIN -32768
#define MAXLIN 32767
#define LINCLIP(x) do { if ( x < MINLIN ) x = MINLIN ; else if ( x > MAXLIN ) x = MAXLIN; } while ( 0 )
#define LINCLIP(x) do { if ( x < MINLIN ) x = MINLIN ; \
else if ( x > MAXLIN ) x = MAXLIN; \
} while ( 0 )
static unsigned char st_linear_to_ulaw( /* int sample */ );
@ -104,8 +105,8 @@ static int ulaw_table[256] = {
static unsigned char
st_linear_to_ulaw( sample )
int sample;
{
int sample;
{
static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
@ -140,7 +141,7 @@ int sample;
#endif
return ulawbyte;
}
}
/* End of code taken from sox */
/* Intel ADPCM step variation table */
@ -167,47 +168,47 @@ static int stepsizeTable[89] = {
static object *AudioopError;
static PyObject *AudioopError;
static object *
static PyObject *
audioop_getsample(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0;
int i;
if ( !getargs(args, "(s#ii)", &cp, &len, &size, &i) )
if ( !PyArg_Parse(args, "(s#ii)", &cp, &len, &size, &i) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
if ( i < 0 || i >= len/size ) {
err_setstr(AudioopError, "Index out of range");
PyErr_SetString(AudioopError, "Index out of range");
return 0;
}
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);
return newintobject(val);
return PyInt_FromLong(val);
}
static object *
static PyObject *
audioop_max(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0;
int i;
int max = 0;
if ( !getargs(args, "(s#i)", &cp, &len, &size) )
if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
for ( i=0; i<len; i+= size) {
@ -217,23 +218,23 @@ audioop_max(self, args)
if ( val < 0 ) val = (-val);
if ( val > max ) max = val;
}
return newintobject(max);
return PyInt_FromLong(max);
}
static object *
static PyObject *
audioop_minmax(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0;
int i;
int min = 0x7fffffff, max = -0x7fffffff;
if (!getargs(args, "(s#i)", &cp, &len, &size))
if (!PyArg_Parse(args, "(s#i)", &cp, &len, &size))
return NULL;
if (size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return NULL;
}
for (i = 0; i < len; i += size) {
@ -243,23 +244,23 @@ audioop_minmax(self, args)
if (val > max) max = val;
if (val < min) min = val;
}
return mkvalue("(ii)", min, max);
return Py_BuildValue("(ii)", min, max);
}
static object *
static PyObject *
audioop_avg(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0;
int i;
float avg = 0.0;
if ( !getargs(args, "(s#i)", &cp, &len, &size) )
if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
for ( i=0; i<len; i+= size) {
@ -272,23 +273,23 @@ audioop_avg(self, args)
val = 0;
else
val = (int)(avg / (float)(len/size));
return newintobject(val);
return PyInt_FromLong(val);
}
static object *
static PyObject *
audioop_rms(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0;
int i;
float sum_squares = 0.0;
if ( !getargs(args, "(s#i)", &cp, &len, &size) )
if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
for ( i=0; i<len; i+= size) {
@ -301,13 +302,13 @@ audioop_rms(self, args)
val = 0;
else
val = (int)sqrt(sum_squares / (float)(len/size));
return newintobject(val);
return PyInt_FromLong(val);
}
static double _sum2(a, b, len)
short *a;
short *b;
int len;
short *b;
int len;
{
int i;
double sum = 0.0;
@ -350,10 +351,10 @@ static double _sum2(a, b, len)
** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
** is completely recalculated each step.
*/
static object *
static PyObject *
audioop_findfit(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
short *cp1, *cp2;
int len1, len2;
@ -361,17 +362,17 @@ audioop_findfit(self, args)
double aj_m1, aj_lm1;
double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
if ( !getargs(args, "(s#s#)", &cp1, &len1, &cp2, &len2) )
if ( !PyArg_Parse(args, "(s#s#)", &cp1, &len1, &cp2, &len2) )
return 0;
if ( len1 & 1 || len2 & 1 ) {
err_setstr(AudioopError, "Strings should be even-sized");
PyErr_SetString(AudioopError, "Strings should be even-sized");
return 0;
}
len1 >>= 1;
len2 >>= 1;
if ( len1 < len2 ) {
err_setstr(AudioopError, "First sample should be longer");
PyErr_SetString(AudioopError, "First sample should be longer");
return 0;
}
sum_ri_2 = _sum2(cp2, cp2, len2);
@ -391,7 +392,8 @@ audioop_findfit(self, args)
sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
sum_aij_ri = _sum2(cp1+j, cp2, len2);
result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
/ sum_aij_2;
if ( result < best_result ) {
best_result = result;
@ -402,30 +404,30 @@ audioop_findfit(self, args)
factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
return mkvalue("(if)", best_j, factor);
return Py_BuildValue("(if)", best_j, factor);
}
/*
** findfactor finds a factor f so that the energy in A-fB is minimal.
** See the comment for findfit for details.
*/
static object *
static PyObject *
audioop_findfactor(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
short *cp1, *cp2;
int len1, len2;
double sum_ri_2, sum_aij_ri, result;
if ( !getargs(args, "(s#s#)", &cp1, &len1, &cp2, &len2) )
if ( !PyArg_Parse(args, "(s#s#)", &cp1, &len1, &cp2, &len2) )
return 0;
if ( len1 & 1 || len2 & 1 ) {
err_setstr(AudioopError, "Strings should be even-sized");
PyErr_SetString(AudioopError, "Strings should be even-sized");
return 0;
}
if ( len1 != len2 ) {
err_setstr(AudioopError, "Samples should be same size");
PyErr_SetString(AudioopError, "Samples should be same size");
return 0;
}
len2 >>= 1;
@ -434,17 +436,17 @@ audioop_findfactor(self, args)
result = sum_aij_ri / sum_ri_2;
return newfloatobject(result);
return PyFloat_FromDouble(result);
}
/*
** findmax returns the index of the n-sized segment of the input sample
** that contains the most energy.
*/
static object *
static PyObject *
audioop_findmax(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
short *cp1;
int len1, len2;
@ -452,16 +454,16 @@ audioop_findmax(self, args)
double aj_m1, aj_lm1;
double result, best_result;
if ( !getargs(args, "(s#i)", &cp1, &len1, &len2) )
if ( !PyArg_Parse(args, "(s#i)", &cp1, &len1, &len2) )
return 0;
if ( len1 & 1 ) {
err_setstr(AudioopError, "Strings should be even-sized");
PyErr_SetString(AudioopError, "Strings should be even-sized");
return 0;
}
len1 >>= 1;
if ( len1 < len2 ) {
err_setstr(AudioopError, "Input sample should be longer");
PyErr_SetString(AudioopError, "Input sample should be longer");
return 0;
}
@ -484,24 +486,25 @@ audioop_findmax(self, args)
}
return newintobject(best_j);
return PyInt_FromLong(best_j);
}
static object *
static PyObject *
audioop_avgpp(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0, prevval = 0, prevextremevalid = 0, prevextreme = 0;
int len, size, val = 0, prevval = 0, prevextremevalid = 0,
prevextreme = 0;
int i;
float avg = 0.0;
int diff, prevdiff, extremediff, nextreme = 0;
if ( !getargs(args, "(s#i)", &cp, &len, &size) )
if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
/* Compute first delta value ahead. Also automatically makes us
@ -521,8 +524,8 @@ audioop_avgpp(self, args)
else if ( size == 4 ) val = (int)*LONGP(cp, i);
diff = val - prevval;
if ( diff*prevdiff < 0 ) {
/* Derivative changed sign. Compute difference to last extreme
** value and remember.
/* Derivative changed sign. Compute difference to last
** extreme value and remember.
*/
if ( prevextremevalid ) {
extremediff = prevval - prevextreme;
@ -542,24 +545,25 @@ audioop_avgpp(self, args)
val = 0;
else
val = (int)(avg / (float)nextreme);
return newintobject(val);
return PyInt_FromLong(val);
}
static object *
static PyObject *
audioop_maxpp(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0, prevval = 0, prevextremevalid = 0, prevextreme = 0;
int len, size, val = 0, prevval = 0, prevextremevalid = 0,
prevextreme = 0;
int i;
int max = 0;
int diff, prevdiff, extremediff;
if ( !getargs(args, "(s#i)", &cp, &len, &size) )
if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
/* Compute first delta value ahead. Also automatically makes us
@ -579,8 +583,8 @@ audioop_maxpp(self, args)
else if ( size == 4 ) val = (int)*LONGP(cp, i);
diff = val - prevval;
if ( diff*prevdiff < 0 ) {
/* Derivative changed sign. Compute difference to last extreme
** value and remember.
/* Derivative changed sign. Compute difference to
** last extreme value and remember.
*/
if ( prevextremevalid ) {
extremediff = prevval - prevextreme;
@ -596,23 +600,23 @@ audioop_maxpp(self, args)
if ( diff != 0 )
prevdiff = diff;
}
return newintobject(max);
return PyInt_FromLong(max);
}
static object *
static PyObject *
audioop_cross(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
int len, size, val = 0;
int i;
int prevval, ncross;
if ( !getargs(args, "(s#i)", &cp, &len, &size) )
if ( !PyArg_Parse(args, "(s#i)", &cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
ncross = -1;
@ -625,35 +629,35 @@ audioop_cross(self, args)
if ( val != prevval ) ncross++;
prevval = val;
}
return newintobject(ncross);
return PyInt_FromLong(ncross);
}
static object *
static PyObject *
audioop_mul(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp, *ncp;
int len, size, val = 0;
double factor, fval, maxval;
object *rv;
PyObject *rv;
int i;
if ( !getargs(args, "(s#id)", &cp, &len, &size, &factor ) )
if ( !PyArg_Parse(args, "(s#id)", &cp, &len, &size, &factor ) )
return 0;
if ( size == 1 ) maxval = (double) 0x7f;
else if ( size == 2 ) maxval = (double) 0x7fff;
else if ( size == 4 ) maxval = (double) 0x7fffffff;
else {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len);
rv = PyString_FromStringAndSize(NULL, len);
if ( rv == 0 )
return 0;
ncp = (signed char *)getstringvalue(rv);
ncp = (signed char *)PyString_AsString(rv);
for ( i=0; i < len; i += size ) {
@ -671,32 +675,32 @@ audioop_mul(self, args)
return rv;
}
static object *
static PyObject *
audioop_tomono(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp, *ncp;
int len, size, val1 = 0, val2 = 0;
double fac1, fac2, fval, maxval;
object *rv;
PyObject *rv;
int i;
if ( !getargs(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
if ( !PyArg_Parse(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
return 0;
if ( size == 1 ) maxval = (double) 0x7f;
else if ( size == 2 ) maxval = (double) 0x7fff;
else if ( size == 4 ) maxval = (double) 0x7fffffff;
else {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len/2);
rv = PyString_FromStringAndSize(NULL, len/2);
if ( rv == 0 )
return 0;
ncp = (signed char *)getstringvalue(rv);
ncp = (signed char *)PyString_AsString(rv);
for ( i=0; i < len; i += size*2 ) {
@ -717,32 +721,32 @@ audioop_tomono(self, args)
return rv;
}
static object *
static PyObject *
audioop_tostereo(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp, *ncp;
int len, size, val1, val2, val = 0;
double fac1, fac2, fval, maxval;
object *rv;
PyObject *rv;
int i;
if ( !getargs(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
if ( !PyArg_Parse(args, "(s#idd)", &cp, &len, &size, &fac1, &fac2 ) )
return 0;
if ( size == 1 ) maxval = (double) 0x7f;
else if ( size == 2 ) maxval = (double) 0x7fff;
else if ( size == 4 ) maxval = (double) 0x7fffffff;
else {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len*2);
rv = PyString_FromStringAndSize(NULL, len*2);
if ( rv == 0 )
return 0;
ncp = (signed char *)getstringvalue(rv);
ncp = (signed char *)PyString_AsString(rv);
for ( i=0; i < len; i += size ) {
@ -771,34 +775,34 @@ audioop_tostereo(self, args)
return rv;
}
static object *
static PyObject *
audioop_add(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp1, *cp2, *ncp;
int len1, len2, size, val1 = 0, val2 = 0;
object *rv;
PyObject *rv;
int i;
if ( !getargs(args, "(s#s#i)",
if ( !PyArg_Parse(args, "(s#s#i)",
&cp1, &len1, &cp2, &len2, &size ) )
return 0;
if ( len1 != len2 ) {
err_setstr(AudioopError, "Lengths should be the same");
PyErr_SetString(AudioopError, "Lengths should be the same");
return 0;
}
if ( size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len1);
rv = PyString_FromStringAndSize(NULL, len1);
if ( rv == 0 )
return 0;
ncp = (signed char *)getstringvalue(rv);
ncp = (signed char *)PyString_AsString(rv);
for ( i=0; i < len1; i += size ) {
@ -817,30 +821,30 @@ audioop_add(self, args)
return rv;
}
static object *
static PyObject *
audioop_bias(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp, *ncp;
int len, size, val = 0;
object *rv;
PyObject *rv;
int i;
int bias;
if ( !getargs(args, "(s#ii)",
if ( !PyArg_Parse(args, "(s#ii)",
&cp, &len, &size , &bias) )
return 0;
if ( size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len);
rv = PyString_FromStringAndSize(NULL, len);
if ( rv == 0 )
return 0;
ncp = (signed char *)getstringvalue(rv);
ncp = (signed char *)PyString_AsString(rv);
for ( i=0; i < len; i += size ) {
@ -855,30 +859,30 @@ audioop_bias(self, args)
return rv;
}
static object *
static PyObject *
audioop_reverse(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
unsigned char *ncp;
int len, size, val = 0;
object *rv;
PyObject *rv;
int i, j;
if ( !getargs(args, "(s#i)",
if ( !PyArg_Parse(args, "(s#i)",
&cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4 ) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len);
rv = PyString_FromStringAndSize(NULL, len);
if ( rv == 0 )
return 0;
ncp = (unsigned char *)getstringvalue(rv);
ncp = (unsigned char *)PyString_AsString(rv);
for ( i=0; i < len; i += size ) {
if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
@ -894,31 +898,31 @@ audioop_reverse(self, args)
return rv;
}
static object *
static PyObject *
audioop_lin2lin(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
unsigned char *ncp;
int len, size, size2, val = 0;
object *rv;
PyObject *rv;
int i, j;
if ( !getargs(args, "(s#ii)",
if ( !PyArg_Parse(args, "(s#ii)",
&cp, &len, &size, &size2) )
return 0;
if ( (size != 1 && size != 2 && size != 4) ||
(size2 != 1 && size2 != 2 && size2 != 4)) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, (len/size)*size2);
rv = PyString_FromStringAndSize(NULL, (len/size)*size2);
if ( rv == 0 )
return 0;
ncp = (unsigned char *)getstringvalue(rv);
ncp = (unsigned char *)PyString_AsString(rv);
for ( i=0, j=0; i < len; i += size, j += size2 ) {
if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
@ -932,30 +936,30 @@ audioop_lin2lin(self, args)
return rv;
}
static object *
static PyObject *
audioop_lin2ulaw(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
unsigned char *ncp;
int len, size, val = 0;
object *rv;
PyObject *rv;
int i;
if ( !getargs(args, "(s#i)",
if ( !PyArg_Parse(args, "(s#i)",
&cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len/size);
rv = PyString_FromStringAndSize(NULL, len/size);
if ( rv == 0 )
return 0;
ncp = (unsigned char *)getstringvalue(rv);
ncp = (unsigned char *)PyString_AsString(rv);
for ( i=0; i < len; i += size ) {
if ( size == 1 ) val = ((int)*CHARP(cp, i)) << 8;
@ -967,31 +971,31 @@ audioop_lin2ulaw(self, args)
return rv;
}
static object *
static PyObject *
audioop_ulaw2lin(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
unsigned char *cp;
unsigned char cval;
signed char *ncp;
int len, size, val;
object *rv;
PyObject *rv;
int i;
if ( !getargs(args, "(s#i)",
if ( !PyArg_Parse(args, "(s#i)",
&cp, &len, &size) )
return 0;
if ( size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
rv = newsizedstringobject(NULL, len*size);
rv = PyString_FromStringAndSize(NULL, len*size);
if ( rv == 0 )
return 0;
ncp = (signed char *)getstringvalue(rv);
ncp = (signed char *)PyString_AsString(rv);
for ( i=0; i < len*size; i += size ) {
cval = *cp++;
@ -1004,39 +1008,40 @@ audioop_ulaw2lin(self, args)
return rv;
}
static object *
static PyObject *
audioop_lin2adpcm(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
signed char *ncp;
int len, size, val = 0, step, valpred, delta, index, sign, vpdiff, diff;
object *rv, *state, *str;
int len, size, val = 0, step, valpred, delta,
index, sign, vpdiff, diff;
PyObject *rv, *state, *str;
int i, outputbuffer = 0, bufferstep;
if ( !getargs(args, "(s#iO)",
if ( !PyArg_Parse(args, "(s#iO)",
&cp, &len, &size, &state) )
return 0;
if ( size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
str = newsizedstringobject(NULL, len/(size*2));
str = PyString_FromStringAndSize(NULL, len/(size*2));
if ( str == 0 )
return 0;
ncp = (signed char *)getstringvalue(str);
ncp = (signed char *)PyString_AsString(str);
/* Decode state, should have (value, step) */
if ( state == None ) {
if ( state == Py_None ) {
/* First time, it seems. Set defaults */
valpred = 0;
step = 7;
index = 0;
} else if ( !getargs(state, "(ii)", &valpred, &index) )
} else if ( !PyArg_Parse(state, "(ii)", &valpred, &index) )
return 0;
step = stepsizeTable[index];
@ -1057,9 +1062,9 @@ audioop_lin2adpcm(self, args)
** This code *approximately* computes:
** delta = diff*4/step;
** vpdiff = (delta+0.5)*step/4;
** but in shift step bits are dropped. The net result of this is
** that even if you have fast mul/div hardware you cannot put it to
** good use since the fixup would be too expensive.
** but in shift step bits are dropped. The net result of this
** is that even if you have fast mul/div hardware you cannot
** put it to good use since the fixup would be too expensive.
*/
delta = 0;
vpdiff = (step >> 3);
@ -1109,44 +1114,44 @@ audioop_lin2adpcm(self, args)
}
bufferstep = !bufferstep;
}
rv = mkvalue("(O(ii))", str, valpred, index);
DECREF(str);
rv = Py_BuildValue("(O(ii))", str, valpred, index);
Py_DECREF(str);
return rv;
}
static object *
static PyObject *
audioop_adpcm2lin(self, args)
object *self;
object *args;
PyObject *self;
PyObject *args;
{
signed char *cp;
signed char *ncp;
int len, size, valpred, step, delta, index, sign, vpdiff;
object *rv, *str, *state;
PyObject *rv, *str, *state;
int i, inputbuffer = 0, bufferstep;
if ( !getargs(args, "(s#iO)",
if ( !PyArg_Parse(args, "(s#iO)",
&cp, &len, &size, &state) )
return 0;
if ( size != 1 && size != 2 && size != 4) {
err_setstr(AudioopError, "Size should be 1, 2 or 4");
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
return 0;
}
/* Decode state, should have (value, step) */
if ( state == None ) {
if ( state == Py_None ) {
/* First time, it seems. Set defaults */
valpred = 0;
step = 7;
index = 0;
} else if ( !getargs(state, "(ii)", &valpred, &index) )
} else if ( !PyArg_Parse(state, "(ii)", &valpred, &index) )
return 0;
str = newsizedstringobject(NULL, len*size*2);
str = PyString_FromStringAndSize(NULL, len*size*2);
if ( str == 0 )
return 0;
ncp = (signed char *)getstringvalue(str);
ncp = (signed char *)PyString_AsString(str);
step = stepsizeTable[index];
bufferstep = 0;
@ -1201,12 +1206,12 @@ audioop_adpcm2lin(self, args)
else if ( size == 4 ) *LONGP(ncp, i) = (long)(valpred<<16);
}
rv = mkvalue("(O(ii))", str, valpred, index);
DECREF(str);
rv = Py_BuildValue("(O(ii))", str, valpred, index);
Py_DECREF(str);
return rv;
}
static struct methodlist audioop_methods[] = {
static PyMethodDef audioop_methods[] = {
{ "max", audioop_max },
{ "minmax", audioop_minmax },
{ "avg", audioop_avg },
@ -1236,10 +1241,11 @@ static struct methodlist audioop_methods[] = {
void
initaudioop()
{
object *m, *d;
m = initmodule("audioop", audioop_methods);
d = getmoduledict(m);
AudioopError = newstringobject("audioop.error");
if ( AudioopError == NULL || dictinsert(d,"error",AudioopError) )
fatal("can't define audioop.error");
PyObject *m, *d;
m = Py_InitModule("audioop", audioop_methods);
d = PyModule_GetDict(m);
AudioopError = PyString_FromString("audioop.error");
if ( AudioopError == NULL
|| PyDict_SetItemString(d,"error",AudioopError) )
Py_FatalError("can't define audioop.error");
}