Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Modules/audioop.c

    r2 r388  
    2424#endif
    2525#endif
     26
     27static const int maxvals[] = {0, 0x7F, 0x7FFF, 0x7FFFFF, 0x7FFFFFFF};
     28static const int minvals[] = {0, -0x80, -0x8000, -0x800000, -0x80000000};
     29static const unsigned int masks[] = {0, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF};
     30
     31static int
     32fbound(double val, double minval, double maxval)
     33{
     34    if (val > maxval)
     35        val = maxval;
     36    else if (val < minval + 1)
     37        val = minval;
     38    return val;
     39}
     40
    2641
    2742/* Code shamelessly stolen from sox, 12.17.7, g711.c
     
    5469search(PyInt16 val, PyInt16 *table, int size)
    5570{
    56         int i;
    57 
    58         for (i = 0; i < size; i++) {
    59                 if (val <= *table++)
    60                         return (i);
    61         }
    62         return (size);
     71    int i;
     72
     73    for (i = 0; i < size; i++) {
     74        if (val <= *table++)
     75            return (i);
     76    }
     77    return (size);
    6378}
    6479#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
     
    8499      -120,    -112,    -104,     -96,     -88,     -80,     -72,
    85100       -64,     -56,     -48,     -40,     -32,     -24,     -16,
    86         -8,       0,   32124,   31100,   30076,   29052,   28028,
     101    -8,       0,   32124,   31100,   30076,   29052,   28028,
    87102     27004,   25980,   24956,   23932,   22908,   21884,   20860,
    88103     19836,   18812,   17788,   16764,   15996,   15484,   14972,
     
    101116       260,     244,     228,     212,     196,     180,     164,
    102117       148,     132,     120,     112,     104,      96,      88,
    103         80,      72,      64,      56,      48,      40,      32,
    104         24,      16,       8,       0
     118    80,      72,      64,      56,      48,      40,      32,
     119    24,      16,       8,       0
    105120};
    106121
     
    138153 */
    139154static unsigned char
    140 st_14linear2ulaw(PyInt16 pcm_val)       /* 2's complement (14-bit range) */
    141 {
    142         PyInt16         mask;
    143         PyInt16         seg;
    144         unsigned char   uval;
    145 
    146         /* The original sox code does this in the calling function, not here */
    147         pcm_val = pcm_val >> 2;
    148 
    149         /* u-law inverts all bits */
    150         /* Get the sign and the magnitude of the value. */
    151         if (pcm_val < 0) {
    152                 pcm_val = -pcm_val;
    153                 mask = 0x7F;
    154         } else {
    155                 mask = 0xFF;
    156         }
    157         if ( pcm_val > CLIP ) pcm_val = CLIP;           /* clip the magnitude */
    158         pcm_val += (BIAS >> 2);
    159 
    160         /* Convert the scaled magnitude to segment number. */
    161         seg = search(pcm_val, seg_uend, 8);
    162 
    163         /*
    164          * Combine the sign, segment, quantization bits;
    165          * and complement the code word.
    166          */
    167         if (seg >= 8)           /* out of range, return maximum value. */
    168                 return (unsigned char) (0x7F ^ mask);
    169         else {
    170                 uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
    171                 return (uval ^ mask);
    172         }
     155st_14linear2ulaw(PyInt16 pcm_val)       /* 2's complement (14-bit range) */
     156{
     157    PyInt16         mask;
     158    PyInt16         seg;
     159    unsigned char   uval;
     160
     161    /* The original sox code does this in the calling function, not here */
     162    pcm_val = pcm_val >> 2;
     163
     164    /* u-law inverts all bits */
     165    /* Get the sign and the magnitude of the value. */
     166    if (pcm_val < 0) {
     167        pcm_val = -pcm_val;
     168        mask = 0x7F;
     169    } else {
     170        mask = 0xFF;
     171    }
     172    if ( pcm_val > CLIP ) pcm_val = CLIP;           /* clip the magnitude */
     173    pcm_val += (BIAS >> 2);
     174
     175    /* Convert the scaled magnitude to segment number. */
     176    seg = search(pcm_val, seg_uend, 8);
     177
     178    /*
     179     * Combine the sign, segment, quantization bits;
     180     * and complement the code word.
     181     */
     182    if (seg >= 8)           /* out of range, return maximum value. */
     183        return (unsigned char) (0x7F ^ mask);
     184    else {
     185        uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
     186        return (uval ^ mask);
     187    }
    173188
    174189}
     
    235250 */
    236251static unsigned char
    237 st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
    238 {
    239         PyInt16         mask;
    240         short           seg;
    241         unsigned char   aval;
    242 
    243         /* The original sox code does this in the calling function, not here */
    244         pcm_val = pcm_val >> 3;
    245 
    246         /* A-law using even bit inversion */
    247         if (pcm_val >= 0) {
    248                 mask = 0xD5;            /* sign (7th) bit = 1 */
    249         } else {
    250                 mask = 0x55;            /* sign bit = 0 */
    251                 pcm_val = -pcm_val - 1;
    252         }
    253 
    254         /* Convert the scaled magnitude to segment number. */
    255         seg = search(pcm_val, seg_aend, 8);
    256 
    257         /* Combine the sign, segment, and quantization bits. */
    258 
    259         if (seg >= 8)           /* out of range, return maximum value. */
    260                 return (unsigned char) (0x7F ^ mask);
    261         else {
    262                 aval = (unsigned char) seg << SEG_SHIFT;
    263                 if (seg < 2)
    264                         aval |= (pcm_val >> 1) & QUANT_MASK;
    265                 else
    266                         aval |= (pcm_val >> seg) & QUANT_MASK;
    267                 return (aval ^ mask);
    268         }
     252st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
     253{
     254    PyInt16         mask;
     255    short           seg;
     256    unsigned char   aval;
     257
     258    /* The original sox code does this in the calling function, not here */
     259    pcm_val = pcm_val >> 3;
     260
     261    /* A-law using even bit inversion */
     262    if (pcm_val >= 0) {
     263        mask = 0xD5;            /* sign (7th) bit = 1 */
     264    } else {
     265        mask = 0x55;            /* sign bit = 0 */
     266        pcm_val = -pcm_val - 1;
     267    }
     268
     269    /* Convert the scaled magnitude to segment number. */
     270    seg = search(pcm_val, seg_aend, 8);
     271
     272    /* Combine the sign, segment, and quantization bits. */
     273
     274    if (seg >= 8)           /* out of range, return maximum value. */
     275        return (unsigned char) (0x7F ^ mask);
     276    else {
     277        aval = (unsigned char) seg << SEG_SHIFT;
     278        if (seg < 2)
     279            aval |= (pcm_val >> 1) & QUANT_MASK;
     280        else
     281            aval |= (pcm_val >> seg) & QUANT_MASK;
     282        return (aval ^ mask);
     283    }
    269284}
    270285/* End of code taken from sox */
     
    272287/* Intel ADPCM step variation table */
    273288static int indexTable[16] = {
    274         -1, -1, -1, -1, 2, 4, 6, 8,
    275         -1, -1, -1, -1, 2, 4, 6, 8,
     289    -1, -1, -1, -1, 2, 4, 6, 8,
     290    -1, -1, -1, -1, 2, 4, 6, 8,
    276291};
    277292
    278293static int stepsizeTable[89] = {
    279         7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
    280         19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
    281         50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
    282         130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
    283         337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
    284         876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
    285         2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
    286         5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
    287         15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
     294    7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
     295    19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
     296    50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
     297    130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
     298    337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
     299    876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
     300    2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
     301    5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
     302    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
    288303};
    289    
     304
    290305#define CHARP(cp, i) ((signed char *)(cp+i))
    291306#define SHORTP(cp, i) ((short *)(cp+i))
     
    296311static PyObject *AudioopError;
    297312
     313static int
     314audioop_check_size(int size)
     315{
     316    if (size != 1 && size != 2 && size != 4) {
     317        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
     318        return 0;
     319    }
     320    else
     321        return 1;
     322}
     323
     324static int
     325audioop_check_parameters(int len, int size)
     326{
     327    if (!audioop_check_size(size))
     328        return 0;
     329    if (len % size != 0) {
     330        PyErr_SetString(AudioopError, "not a whole number of frames");
     331        return 0;
     332    }
     333    return 1;
     334}
     335
    298336static PyObject *
    299337audioop_getsample(PyObject *self, PyObject *args)
    300338{
    301         signed char *cp;
    302         int len, size, val = 0;
    303         int i;
    304 
    305         if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
    306                 return 0;
    307         if ( size != 1 && size != 2 && size != 4 ) {
    308                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    309                 return 0;
    310         }
    311         if ( i < 0 || i >= len/size ) {
    312                 PyErr_SetString(AudioopError, "Index out of range");
    313                 return 0;
    314         }
     339    signed char *cp;
     340    int len, size, val = 0;
     341    int i;
     342
     343    if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
     344        return 0;
     345    if (!audioop_check_parameters(len, size))
     346        return NULL;
     347    if ( i < 0 || i >= len/size ) {
     348        PyErr_SetString(AudioopError, "Index out of range");
     349        return 0;
     350    }
     351    if ( size == 1 )      val = (int)*CHARP(cp, i);
     352    else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
     353    else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
     354    return PyInt_FromLong(val);
     355}
     356
     357static PyObject *
     358audioop_max(PyObject *self, PyObject *args)
     359{
     360    signed char *cp;
     361    int len, size, val = 0;
     362    int i;
     363    unsigned int absval, max = 0;
     364
     365    if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
     366        return 0;
     367    if (!audioop_check_parameters(len, size))
     368        return NULL;
     369    for ( i=0; i<len; i+= size) {
    315370        if ( size == 1 )      val = (int)*CHARP(cp, i);
    316         else if ( size == 2 ) val = (int)*SHORTP(cp, i*2);
    317         else if ( size == 4 ) val = (int)*LONGP(cp, i*4);
    318         return PyInt_FromLong(val);
    319 }
    320 
    321 static PyObject *
    322 audioop_max(PyObject *self, PyObject *args)
    323 {
    324         signed char *cp;
    325         int len, size, val = 0;
    326         int i;
    327         int max = 0;
    328 
    329         if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
    330                 return 0;
    331         if ( size != 1 && size != 2 && size != 4 ) {
    332                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    333                 return 0;
    334         }
    335         for ( i=0; i<len; i+= size) {
    336                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    337                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    338                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    339                 if ( val < 0 ) val = (-val);
    340                 if ( val > max ) max = val;
    341         }
     371        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     372        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     373        if (val < 0) absval = (-val);
     374        else absval = val;
     375        if (absval > max) max = absval;
     376    }
     377    if (max <= INT_MAX)
    342378        return PyInt_FromLong(max);
     379    else
     380        return PyLong_FromUnsignedLong(max);
    343381}
    344382
     
    346384audioop_minmax(PyObject *self, PyObject *args)
    347385{
    348         signed char *cp;
    349         int len, size, val = 0;
    350         int i;
    351         int min = 0x7fffffff, max = -0x7fffffff;
    352 
    353         if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
    354                 return NULL;
    355         if (size != 1 && size != 2 && size != 4) {
    356                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    357                 return NULL;
    358         }
    359         for (i = 0; i < len; i += size) {
    360                 if (size == 1) val = (int) *CHARP(cp, i);
    361                 else if (size == 2) val = (int) *SHORTP(cp, i);
    362                 else if (size == 4) val = (int) *LONGP(cp, i);
    363                 if (val > max) max = val;
    364                 if (val < min) min = val;
    365         }
    366         return Py_BuildValue("(ii)", min, max);
     386    signed char *cp;
     387    int len, size, val = 0;
     388    int i;
     389    int min = 0x7fffffff, max = -0x80000000;
     390
     391    if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
     392        return NULL;
     393    if (!audioop_check_parameters(len, size))
     394        return NULL;
     395    for (i = 0; i < len; i += size) {
     396        if (size == 1) val = (int) *CHARP(cp, i);
     397        else if (size == 2) val = (int) *SHORTP(cp, i);
     398        else if (size == 4) val = (int) *LONGP(cp, i);
     399        if (val > max) max = val;
     400        if (val < min) min = val;
     401    }
     402    return Py_BuildValue("(ii)", min, max);
    367403}
    368404
     
    370406audioop_avg(PyObject *self, PyObject *args)
    371407{
    372         signed char *cp;
    373         int len, size, val = 0;
    374         int i;
    375         double avg = 0.0;
    376 
    377         if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
    378                 return 0;
    379         if ( size != 1 && size != 2 && size != 4 ) {
    380                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    381                 return 0;
    382         }
    383         for ( i=0; i<len; i+= size) {
    384                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    385                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    386                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    387                 avg += val;
    388         }
    389         if ( len == 0 )
    390                 val = 0;
    391         else
    392                 val = (int)(avg / (double)(len/size));
    393         return PyInt_FromLong(val);
     408    signed char *cp;
     409    int len, size, val = 0;
     410    int i;
     411    double avg = 0.0;
     412
     413    if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
     414        return 0;
     415    if (!audioop_check_parameters(len, size))
     416        return NULL;
     417    for ( i=0; i<len; i+= size) {
     418        if ( size == 1 )      val = (int)*CHARP(cp, i);
     419        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     420        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     421        avg += val;
     422    }
     423    if ( len == 0 )
     424        val = 0;
     425    else
     426        val = (int)floor(avg / (double)(len/size));
     427    return PyInt_FromLong(val);
    394428}
    395429
     
    397431audioop_rms(PyObject *self, PyObject *args)
    398432{
    399         signed char *cp;
    400         int len, size, val = 0;
    401         int i;
    402         double sum_squares = 0.0;
    403 
    404         if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
    405                 return 0;
    406         if ( size != 1 && size != 2 && size != 4 ) {
    407                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    408                 return 0;
    409         }
    410         for ( i=0; i<len; i+= size) {
    411                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    412                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    413                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    414                 sum_squares += (double)val*(double)val;
    415         }
    416         if ( len == 0 )
    417                 val = 0;
    418         else
    419                 val = (int)sqrt(sum_squares / (double)(len/size));
    420         return PyInt_FromLong(val);
     433    signed char *cp;
     434    int len, size, val = 0;
     435    int i;
     436    unsigned int res;
     437    double sum_squares = 0.0;
     438
     439    if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
     440        return 0;
     441    if (!audioop_check_parameters(len, size))
     442        return NULL;
     443    for ( i=0; i<len; i+= size) {
     444        if ( size == 1 )      val = (int)*CHARP(cp, i);
     445        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     446        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     447        sum_squares += (double)val*(double)val;
     448    }
     449    if ( len == 0 )
     450        res = 0;
     451    else
     452        res = (unsigned int)sqrt(sum_squares / (double)(len/size));
     453    if (res <= INT_MAX)
     454        return PyInt_FromLong(res);
     455    else
     456        return PyLong_FromUnsignedLong(res);
    421457}
    422458
    423459static double _sum2(short *a, short *b, int len)
    424460{
    425         int i;
    426         double sum = 0.0;
    427 
    428         for( i=0; i<len; i++) {
    429                 sum = sum + (double)a[i]*(double)b[i];
    430         }
    431         return sum;
     461    int i;
     462    double sum = 0.0;
     463
     464    for( i=0; i<len; i++) {
     465        sum = sum + (double)a[i]*(double)b[i];
     466    }
     467    return sum;
    432468}
    433469
     
    467503audioop_findfit(PyObject *self, PyObject *args)
    468504{
    469         short *cp1, *cp2;
    470         int len1, len2;
    471         int j, best_j;
    472         double aj_m1, aj_lm1;
    473         double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
    474 
    475         /* Passing a short** for an 's' argument is correct only
    476            if the string contents is aligned for interpretation
    477            as short[]. Due to the definition of PyStringObject,
    478            this is currently (Python 2.6) the case. */
    479         if ( !PyArg_ParseTuple(args, "s#s#:findfit",
    480                                (char**)&cp1, &len1, (char**)&cp2, &len2) )
    481                 return 0;
    482         if ( len1 & 1 || len2 & 1 ) {
    483                 PyErr_SetString(AudioopError, "Strings should be even-sized");
    484                 return 0;
     505    short *cp1, *cp2;
     506    int len1, len2;
     507    int j, best_j;
     508    double aj_m1, aj_lm1;
     509    double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
     510
     511    /* Passing a short** for an 's' argument is correct only
     512       if the string contents is aligned for interpretation
     513       as short[]. Due to the definition of PyStringObject,
     514       this is currently (Python 2.6) the case. */
     515    if ( !PyArg_ParseTuple(args, "s#s#:findfit",
     516                           (char**)&cp1, &len1, (char**)&cp2, &len2) )
     517        return 0;
     518    if ( len1 & 1 || len2 & 1 ) {
     519        PyErr_SetString(AudioopError, "Strings should be even-sized");
     520        return 0;
     521    }
     522    len1 >>= 1;
     523    len2 >>= 1;
     524
     525    if ( len1 < len2 ) {
     526        PyErr_SetString(AudioopError, "First sample should be longer");
     527        return 0;
     528    }
     529    sum_ri_2 = _sum2(cp2, cp2, len2);
     530    sum_aij_2 = _sum2(cp1, cp1, len2);
     531    sum_aij_ri = _sum2(cp1, cp2, len2);
     532
     533    result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
     534
     535    best_result = result;
     536    best_j = 0;
     537
     538    for (j=1; j<=len1-len2; j++) {
     539        aj_m1 = (double)cp1[j-1];
     540        aj_lm1 = (double)cp1[j+len2-1];
     541
     542        sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
     543        sum_aij_ri = _sum2(cp1+j, cp2, len2);
     544
     545        result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
     546            / sum_aij_2;
     547
     548        if ( result < best_result ) {
     549            best_result = result;
     550            best_j = j;
    485551        }
    486         len1 >>= 1;
    487         len2 >>= 1;
    488    
    489         if ( len1 < len2 ) {
    490                 PyErr_SetString(AudioopError, "First sample should be longer");
    491                 return 0;
    492         }
    493         sum_ri_2 = _sum2(cp2, cp2, len2);
    494         sum_aij_2 = _sum2(cp1, cp1, len2);
    495         sum_aij_ri = _sum2(cp1, cp2, len2);
    496 
    497         result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
    498 
    499         best_result = result;
    500         best_j = 0;
    501         j = 0;
    502 
    503         for ( j=1; j<=len1-len2; j++) {
    504                 aj_m1 = (double)cp1[j-1];
    505                 aj_lm1 = (double)cp1[j+len2-1];
    506 
    507                 sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
    508                 sum_aij_ri = _sum2(cp1+j, cp2, len2);
    509 
    510                 result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
    511                         / sum_aij_2;
    512 
    513                 if ( result < best_result ) {
    514                         best_result = result;
    515                         best_j = j;
    516                 }
    517        
    518         }
    519 
    520         factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
    521    
    522         return Py_BuildValue("(if)", best_j, factor);
     552
     553    }
     554
     555    factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
     556
     557    return Py_BuildValue("(if)", best_j, factor);
    523558}
    524559
     
    530565audioop_findfactor(PyObject *self, PyObject *args)
    531566{
    532         short *cp1, *cp2;
    533         int len1, len2;
    534         double sum_ri_2, sum_aij_ri, result;
    535 
    536         if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
    537                                (char**)&cp1, &len1, (char**)&cp2, &len2) )
    538                 return 0;
    539         if ( len1 & 1 || len2 & 1 ) {
    540                 PyErr_SetString(AudioopError, "Strings should be even-sized");
    541                 return 0;
    542         }
    543         if ( len1 != len2 ) {
    544                 PyErr_SetString(AudioopError, "Samples should be same size");
    545                 return 0;
    546         }
    547         len2 >>= 1;
    548         sum_ri_2 = _sum2(cp2, cp2, len2);
    549         sum_aij_ri = _sum2(cp1, cp2, len2);
    550 
    551         result = sum_aij_ri / sum_ri_2;
    552 
    553         return PyFloat_FromDouble(result);
     567    short *cp1, *cp2;
     568    int len1, len2;
     569    double sum_ri_2, sum_aij_ri, result;
     570
     571    if ( !PyArg_ParseTuple(args, "s#s#:findfactor",
     572                           (char**)&cp1, &len1, (char**)&cp2, &len2) )
     573        return 0;
     574    if ( len1 & 1 || len2 & 1 ) {
     575        PyErr_SetString(AudioopError, "Strings should be even-sized");
     576        return 0;
     577    }
     578    if ( len1 != len2 ) {
     579        PyErr_SetString(AudioopError, "Samples should be same size");
     580        return 0;
     581    }
     582    len2 >>= 1;
     583    sum_ri_2 = _sum2(cp2, cp2, len2);
     584    sum_aij_ri = _sum2(cp1, cp2, len2);
     585
     586    result = sum_aij_ri / sum_ri_2;
     587
     588    return PyFloat_FromDouble(result);
    554589}
    555590
     
    561596audioop_findmax(PyObject *self, PyObject *args)
    562597{
    563         short *cp1;
    564         int len1, len2;
    565         int j, best_j;
    566         double aj_m1, aj_lm1;
    567         double result, best_result;
    568 
    569         if ( !PyArg_ParseTuple(args, "s#i:findmax",
    570                                (char**)&cp1, &len1, &len2) )
    571                 return 0;
    572         if ( len1 & 1 ) {
    573                 PyErr_SetString(AudioopError, "Strings should be even-sized");
    574                 return 0;
     598    short *cp1;
     599    int len1, len2;
     600    int j, best_j;
     601    double aj_m1, aj_lm1;
     602    double result, best_result;
     603
     604    if ( !PyArg_ParseTuple(args, "s#i:findmax",
     605                           (char**)&cp1, &len1, &len2) )
     606        return 0;
     607    if ( len1 & 1 ) {
     608        PyErr_SetString(AudioopError, "Strings should be even-sized");
     609        return 0;
     610    }
     611    len1 >>= 1;
     612
     613    if ( len2 < 0 || len1 < len2 ) {
     614        PyErr_SetString(AudioopError, "Input sample should be longer");
     615        return 0;
     616    }
     617
     618    result = _sum2(cp1, cp1, len2);
     619
     620    best_result = result;
     621    best_j = 0;
     622
     623    for (j=1; j<=len1-len2; j++) {
     624        aj_m1 = (double)cp1[j-1];
     625        aj_lm1 = (double)cp1[j+len2-1];
     626
     627        result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
     628
     629        if ( result > best_result ) {
     630            best_result = result;
     631            best_j = j;
    575632        }
    576         len1 >>= 1;
    577    
    578         if ( len2 < 0 || len1 < len2 ) {
    579                 PyErr_SetString(AudioopError, "Input sample should be longer");
    580                 return 0;
     633
     634    }
     635
     636    return PyInt_FromLong(best_j);
     637}
     638
     639static PyObject *
     640audioop_avgpp(PyObject *self, PyObject *args)
     641{
     642    signed char *cp;
     643    int len, size, val = 0, prevval = 0, prevextremevalid = 0,
     644        prevextreme = 0;
     645    int i;
     646    double sum = 0.0;
     647    unsigned int avg;
     648    int diff, prevdiff, nextreme = 0;
     649
     650    if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
     651        return 0;
     652    if (!audioop_check_parameters(len, size))
     653        return NULL;
     654    if (len <= size*2)
     655        return PyInt_FromLong(0);
     656    if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
     657    else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
     658    else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
     659    prevdiff = 17; /* Anything != 0, 1 */
     660    for ( i=size; i<len; i+= size) {
     661        if ( size == 1 )      val = (int)*CHARP(cp, i);
     662        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     663        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     664        if (val != prevval) {
     665            diff = val < prevval;
     666            if (prevdiff == !diff) {
     667                /* Derivative changed sign. Compute difference to last
     668                ** extreme value and remember.
     669                */
     670                if (prevextremevalid) {
     671                    sum += fabs((double)prevval - (double)prevextreme);
     672                    nextreme++;
     673                }
     674                prevextremevalid = 1;
     675                prevextreme = prevval;
     676            }
     677            prevval = val;
     678            prevdiff = diff;
    581679        }
    582 
    583         result = _sum2(cp1, cp1, len2);
    584 
    585         best_result = result;
    586         best_j = 0;
    587         j = 0;
    588 
    589         for ( j=1; j<=len1-len2; j++) {
    590                 aj_m1 = (double)cp1[j-1];
    591                 aj_lm1 = (double)cp1[j+len2-1];
    592 
    593                 result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
    594 
    595                 if ( result > best_result ) {
    596                         best_result = result;
    597                         best_j = j;
     680    }
     681    if ( nextreme == 0 )
     682        avg = 0;
     683    else
     684        avg = (unsigned int)(sum / (double)nextreme);
     685    if (avg <= INT_MAX)
     686        return PyInt_FromLong(avg);
     687    else
     688        return PyLong_FromUnsignedLong(avg);
     689}
     690
     691static PyObject *
     692audioop_maxpp(PyObject *self, PyObject *args)
     693{
     694    signed char *cp;
     695    int len, size, val = 0, prevval = 0, prevextremevalid = 0,
     696        prevextreme = 0;
     697    int i;
     698    unsigned int max = 0, extremediff;
     699    int diff, prevdiff;
     700
     701    if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
     702        return 0;
     703    if (!audioop_check_parameters(len, size))
     704        return NULL;
     705    if (len <= size)
     706        return PyInt_FromLong(0);
     707    if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
     708    else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
     709    else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
     710    prevdiff = 17; /* Anything != 0, 1 */
     711    for ( i=size; i<len; i+= size) {
     712        if ( size == 1 )      val = (int)*CHARP(cp, i);
     713        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     714        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     715        if (val != prevval) {
     716            diff = val < prevval;
     717            if (prevdiff == !diff) {
     718                /* Derivative changed sign. Compute difference to
     719                ** last extreme value and remember.
     720                */
     721                if (prevextremevalid) {
     722                    if (prevval < prevextreme)
     723                        extremediff = (unsigned int)prevextreme -
     724                                      (unsigned int)prevval;
     725                    else
     726                        extremediff = (unsigned int)prevval -
     727                                      (unsigned int)prevextreme;
     728                    if ( extremediff > max )
     729                        max = extremediff;
    598730                }
    599        
     731                prevextremevalid = 1;
     732                prevextreme = prevval;
     733            }
     734            prevval = val;
     735            prevdiff = diff;
    600736        }
    601 
    602         return PyInt_FromLong(best_j);
    603 }
    604 
    605 static PyObject *
    606 audioop_avgpp(PyObject *self, PyObject *args)
    607 {
    608         signed char *cp;
    609         int len, size, val = 0, prevval = 0, prevextremevalid = 0,
    610                 prevextreme = 0;
    611         int i;
    612         double avg = 0.0;
    613         int diff, prevdiff, extremediff, nextreme = 0;
    614 
    615         if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
    616                 return 0;
    617         if ( size != 1 && size != 2 && size != 4 ) {
    618                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    619                 return 0;
     737    }
     738    if (max <= INT_MAX)
     739        return PyInt_FromLong(max);
     740    else
     741        return PyLong_FromUnsignedLong(max);
     742}
     743
     744static PyObject *
     745audioop_cross(PyObject *self, PyObject *args)
     746{
     747    signed char *cp;
     748    int len, size, val = 0;
     749    int i;
     750    int prevval, ncross;
     751
     752    if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
     753        return 0;
     754    if (!audioop_check_parameters(len, size))
     755        return NULL;
     756    ncross = -1;
     757    prevval = 17; /* Anything <> 0,1 */
     758    for ( i=0; i<len; i+= size) {
     759        if ( size == 1 )      val = ((int)*CHARP(cp, i)) >> 7;
     760        else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
     761        else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
     762        val = val & 1;
     763        if ( val != prevval ) ncross++;
     764        prevval = val;
     765    }
     766    return PyInt_FromLong(ncross);
     767}
     768
     769static PyObject *
     770audioop_mul(PyObject *self, PyObject *args)
     771{
     772    signed char *cp, *ncp;
     773    int len, size, val = 0;
     774    double factor, fval, maxval, minval;
     775    PyObject *rv;
     776    int i;
     777
     778    if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
     779        return 0;
     780    if (!audioop_check_parameters(len, size))
     781        return NULL;
     782
     783    maxval = (double) maxvals[size];
     784    minval = (double) minvals[size];
     785
     786    rv = PyString_FromStringAndSize(NULL, len);
     787    if ( rv == 0 )
     788        return 0;
     789    ncp = (signed char *)PyString_AsString(rv);
     790
     791
     792    for ( i=0; i < len; i += size ) {
     793        if ( size == 1 )      val = (int)*CHARP(cp, i);
     794        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     795        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     796        fval = (double)val*factor;
     797        val = (int)floor(fbound(fval, minval, maxval));
     798        if ( size == 1 )      *CHARP(ncp, i) = (signed char)val;
     799        else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
     800        else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
     801    }
     802    return rv;
     803}
     804
     805static PyObject *
     806audioop_tomono(PyObject *self, PyObject *args)
     807{
     808    signed char *cp, *ncp;
     809    int len, size, val1 = 0, val2 = 0;
     810    double fac1, fac2, fval, maxval, minval;
     811    PyObject *rv;
     812    int i;
     813
     814    if ( !PyArg_ParseTuple(args, "s#idd:tomono",
     815                           &cp, &len, &size, &fac1, &fac2 ) )
     816        return 0;
     817    if (!audioop_check_parameters(len, size))
     818        return NULL;
     819    if (((len / size) & 1) != 0) {
     820        PyErr_SetString(AudioopError, "not a whole number of frames");
     821        return NULL;
     822    }
     823
     824    maxval = (double) maxvals[size];
     825    minval = (double) minvals[size];
     826
     827    rv = PyString_FromStringAndSize(NULL, len/2);
     828    if ( rv == 0 )
     829        return 0;
     830    ncp = (signed char *)PyString_AsString(rv);
     831
     832
     833    for ( i=0; i < len; i += size*2 ) {
     834        if ( size == 1 )      val1 = (int)*CHARP(cp, i);
     835        else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
     836        else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
     837        if ( size == 1 )      val2 = (int)*CHARP(cp, i+1);
     838        else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
     839        else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
     840        fval = (double)val1*fac1 + (double)val2*fac2;
     841        val1 = (int)floor(fbound(fval, minval, maxval));
     842        if ( size == 1 )      *CHARP(ncp, i/2) = (signed char)val1;
     843        else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
     844        else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
     845    }
     846    return rv;
     847}
     848
     849static PyObject *
     850audioop_tostereo(PyObject *self, PyObject *args)
     851{
     852    signed char *cp, *ncp;
     853    int len, size, val1, val2, val = 0;
     854    double fac1, fac2, fval, maxval, minval;
     855    PyObject *rv;
     856    int i;
     857
     858    if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
     859                           &cp, &len, &size, &fac1, &fac2 ) )
     860        return 0;
     861    if (!audioop_check_parameters(len, size))
     862        return NULL;
     863
     864    maxval = (double) maxvals[size];
     865    minval = (double) minvals[size];
     866
     867    if (len > INT_MAX/2) {
     868        PyErr_SetString(PyExc_MemoryError,
     869                        "not enough memory for output buffer");
     870        return 0;
     871    }
     872
     873    rv = PyString_FromStringAndSize(NULL, len*2);
     874    if ( rv == 0 )
     875        return 0;
     876    ncp = (signed char *)PyString_AsString(rv);
     877
     878
     879    for ( i=0; i < len; i += size ) {
     880        if ( size == 1 )      val = (int)*CHARP(cp, i);
     881        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     882        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     883
     884        fval = (double)val*fac1;
     885        val1 = (int)floor(fbound(fval, minval, maxval));
     886
     887        fval = (double)val*fac2;
     888        val2 = (int)floor(fbound(fval, minval, maxval));
     889
     890        if ( size == 1 )      *CHARP(ncp, i*2) = (signed char)val1;
     891        else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
     892        else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
     893
     894        if ( size == 1 )      *CHARP(ncp, i*2+1) = (signed char)val2;
     895        else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
     896        else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
     897    }
     898    return rv;
     899}
     900
     901static PyObject *
     902audioop_add(PyObject *self, PyObject *args)
     903{
     904    signed char *cp1, *cp2, *ncp;
     905    int len1, len2, size, val1 = 0, val2 = 0, minval, maxval, newval;
     906    PyObject *rv;
     907    int i;
     908
     909    if ( !PyArg_ParseTuple(args, "s#s#i:add",
     910                      &cp1, &len1, &cp2, &len2, &size ) )
     911        return 0;
     912    if (!audioop_check_parameters(len1, size))
     913        return NULL;
     914    if ( len1 != len2 ) {
     915        PyErr_SetString(AudioopError, "Lengths should be the same");
     916        return 0;
     917    }
     918
     919    maxval = maxvals[size];
     920    minval = minvals[size];
     921
     922    rv = PyString_FromStringAndSize(NULL, len1);
     923    if ( rv == 0 )
     924        return 0;
     925    ncp = (signed char *)PyString_AsString(rv);
     926
     927    for ( i=0; i < len1; i += size ) {
     928        if ( size == 1 )      val1 = (int)*CHARP(cp1, i);
     929        else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
     930        else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
     931
     932        if ( size == 1 )      val2 = (int)*CHARP(cp2, i);
     933        else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
     934        else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
     935
     936        if (size < 4) {
     937            newval = val1 + val2;
     938            /* truncate in case of overflow */
     939            if (newval > maxval)
     940                newval = maxval;
     941            else if (newval < minval)
     942                newval = minval;
    620943        }
    621         /* Compute first delta value ahead. Also automatically makes us
    622         ** skip the first extreme value
    623         */
    624         if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
    625         else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
    626         else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
    627         if ( size == 1 )      val = (int)*CHARP(cp, size);
    628         else if ( size == 2 ) val = (int)*SHORTP(cp, size);
    629         else if ( size == 4 ) val = (int)*LONGP(cp, size);
    630         prevdiff = val - prevval;
    631    
    632         for ( i=size; i<len; i+= size) {
    633                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    634                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    635                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    636                 diff = val - prevval;
    637                 if ( diff*prevdiff < 0 ) {
    638                         /* Derivative changed sign. Compute difference to last
    639                         ** extreme value and remember.
    640                         */
    641                         if ( prevextremevalid ) {
    642                                 extremediff = prevval - prevextreme;
    643                                 if ( extremediff < 0 )
    644                                         extremediff = -extremediff;
    645                                 avg += extremediff;
    646                                 nextreme++;
    647                         }
    648                         prevextremevalid = 1;
    649                         prevextreme = prevval;
    650                 }
    651                 prevval = val;
    652                 if ( diff != 0 )
    653                         prevdiff = diff;       
     944        else {
     945            double fval = (double)val1 + (double)val2;
     946            /* truncate in case of overflow */
     947            newval = (int)floor(fbound(fval, minval, maxval));
    654948        }
    655         if ( nextreme == 0 )
    656                 val = 0;
    657         else
    658                 val = (int)(avg / (double)nextreme);
    659         return PyInt_FromLong(val);
    660 }
    661 
    662 static PyObject *
    663 audioop_maxpp(PyObject *self, PyObject *args)
    664 {
    665         signed char *cp;
    666         int len, size, val = 0, prevval = 0, prevextremevalid = 0,
    667                 prevextreme = 0;
    668         int i;
    669         int max = 0;
    670         int diff, prevdiff, extremediff;
    671 
    672         if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
    673                 return 0;
    674         if ( size != 1 && size != 2 && size != 4 ) {
    675                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    676                 return 0;
    677         }
    678         /* Compute first delta value ahead. Also automatically makes us
    679         ** skip the first extreme value
    680         */
    681         if ( size == 1 )      prevval = (int)*CHARP(cp, 0);
    682         else if ( size == 2 ) prevval = (int)*SHORTP(cp, 0);
    683         else if ( size == 4 ) prevval = (int)*LONGP(cp, 0);
    684         if ( size == 1 )      val = (int)*CHARP(cp, size);
    685         else if ( size == 2 ) val = (int)*SHORTP(cp, size);
    686         else if ( size == 4 ) val = (int)*LONGP(cp, size);
    687         prevdiff = val - prevval;
    688 
    689         for ( i=size; i<len; i+= size) {
    690                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    691                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    692                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    693                 diff = val - prevval;
    694                 if ( diff*prevdiff < 0 ) {
    695                         /* Derivative changed sign. Compute difference to
    696                         ** last extreme value and remember.
    697                         */
    698                         if ( prevextremevalid ) {
    699                                 extremediff = prevval - prevextreme;
    700                                 if ( extremediff < 0 )
    701                                         extremediff = -extremediff;
    702                                 if ( extremediff > max )
    703                                         max = extremediff;
    704                         }
    705                         prevextremevalid = 1;
    706                         prevextreme = prevval;
    707                 }
    708                 prevval = val;
    709                 if ( diff != 0 )
    710                         prevdiff = diff;
    711         }
    712         return PyInt_FromLong(max);
    713 }
    714 
    715 static PyObject *
    716 audioop_cross(PyObject *self, PyObject *args)
    717 {
    718         signed char *cp;
    719         int len, size, val = 0;
    720         int i;
    721         int prevval, ncross;
    722 
    723         if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
    724                 return 0;
    725         if ( size != 1 && size != 2 && size != 4 ) {
    726                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    727                 return 0;
    728         }
    729         ncross = -1;
    730         prevval = 17; /* Anything <> 0,1 */
    731         for ( i=0; i<len; i+= size) {
    732                 if ( size == 1 )      val = ((int)*CHARP(cp, i)) >> 7;
    733                 else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) >> 15;
    734                 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 31;
    735                 val = val & 1;
    736                 if ( val != prevval ) ncross++;
    737                 prevval = val;
    738         }
    739         return PyInt_FromLong(ncross);
    740 }
    741 
    742 static PyObject *
    743 audioop_mul(PyObject *self, PyObject *args)
    744 {
    745         signed char *cp, *ncp;
    746         int len, size, val = 0;
    747         double factor, fval, maxval;
    748         PyObject *rv;
    749         int i;
    750 
    751         if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
    752                 return 0;
    753    
    754         if ( size == 1 ) maxval = (double) 0x7f;
    755         else if ( size == 2 ) maxval = (double) 0x7fff;
    756         else if ( size == 4 ) maxval = (double) 0x7fffffff;
    757         else {
    758                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    759                 return 0;
    760         }
    761    
    762         rv = PyString_FromStringAndSize(NULL, len);
    763         if ( rv == 0 )
    764                 return 0;
    765         ncp = (signed char *)PyString_AsString(rv);
    766    
    767    
    768         for ( i=0; i < len; i += size ) {
    769                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    770                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    771                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    772                 fval = (double)val*factor;
    773                 if ( fval > maxval ) fval = maxval;
    774                 else if ( fval < -maxval ) fval = -maxval;
    775                 val = (int)fval;
    776                 if ( size == 1 )      *CHARP(ncp, i) = (signed char)val;
    777                 else if ( size == 2 ) *SHORTP(ncp, i) = (short)val;
    778                 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)val;
    779         }
    780         return rv;
    781 }
    782 
    783 static PyObject *
    784 audioop_tomono(PyObject *self, PyObject *args)
    785 {
    786         signed char *cp, *ncp;
    787         int len, size, val1 = 0, val2 = 0;
    788         double fac1, fac2, fval, maxval;
    789         PyObject *rv;
    790         int i;
    791 
    792         if ( !PyArg_ParseTuple(args, "s#idd:tomono",
    793                                &cp, &len, &size, &fac1, &fac2 ) )
    794                 return 0;
    795    
    796         if ( size == 1 ) maxval = (double) 0x7f;
    797         else if ( size == 2 ) maxval = (double) 0x7fff;
    798         else if ( size == 4 ) maxval = (double) 0x7fffffff;
    799         else {
    800                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    801                 return 0;
    802         }
    803    
    804         rv = PyString_FromStringAndSize(NULL, len/2);
    805         if ( rv == 0 )
    806                 return 0;
    807         ncp = (signed char *)PyString_AsString(rv);
    808    
    809    
    810         for ( i=0; i < len; i += size*2 ) {
    811                 if ( size == 1 )      val1 = (int)*CHARP(cp, i);
    812                 else if ( size == 2 ) val1 = (int)*SHORTP(cp, i);
    813                 else if ( size == 4 ) val1 = (int)*LONGP(cp, i);
    814                 if ( size == 1 )      val2 = (int)*CHARP(cp, i+1);
    815                 else if ( size == 2 ) val2 = (int)*SHORTP(cp, i+2);
    816                 else if ( size == 4 ) val2 = (int)*LONGP(cp, i+4);
    817                 fval = (double)val1*fac1 + (double)val2*fac2;
    818                 if ( fval > maxval ) fval = maxval;
    819                 else if ( fval < -maxval ) fval = -maxval;
    820                 val1 = (int)fval;
    821                 if ( size == 1 )      *CHARP(ncp, i/2) = (signed char)val1;
    822                 else if ( size == 2 ) *SHORTP(ncp, i/2) = (short)val1;
    823                 else if ( size == 4 ) *LONGP(ncp, i/2)= (Py_Int32)val1;
    824         }
    825         return rv;
    826 }
    827 
    828 static PyObject *
    829 audioop_tostereo(PyObject *self, PyObject *args)
    830 {
    831         signed char *cp, *ncp;
    832         int len, new_len, size, val1, val2, val = 0;
    833         double fac1, fac2, fval, maxval;
    834         PyObject *rv;
    835         int i;
    836 
    837         if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
    838                                &cp, &len, &size, &fac1, &fac2 ) )
    839                 return 0;
    840    
    841         if ( size == 1 ) maxval = (double) 0x7f;
    842         else if ( size == 2 ) maxval = (double) 0x7fff;
    843         else if ( size == 4 ) maxval = (double) 0x7fffffff;
    844         else {
    845                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    846                 return 0;
    847         }
    848    
    849         new_len = len*2;
    850         if (new_len < 0) {
    851                 PyErr_SetString(PyExc_MemoryError,
    852                                 "not enough memory for output buffer");
    853                 return 0;
    854         }
    855 
    856         rv = PyString_FromStringAndSize(NULL, new_len);
    857         if ( rv == 0 )
    858                 return 0;
    859         ncp = (signed char *)PyString_AsString(rv);
    860    
    861    
    862         for ( i=0; i < len; i += size ) {
    863                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    864                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    865                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    866 
    867                 fval = (double)val*fac1;
    868                 if ( fval > maxval ) fval = maxval;
    869                 else if ( fval < -maxval ) fval = -maxval;
    870                 val1 = (int)fval;
    871 
    872                 fval = (double)val*fac2;
    873                 if ( fval > maxval ) fval = maxval;
    874                 else if ( fval < -maxval ) fval = -maxval;
    875                 val2 = (int)fval;
    876 
    877                 if ( size == 1 )      *CHARP(ncp, i*2) = (signed char)val1;
    878                 else if ( size == 2 ) *SHORTP(ncp, i*2) = (short)val1;
    879                 else if ( size == 4 ) *LONGP(ncp, i*2) = (Py_Int32)val1;
    880 
    881                 if ( size == 1 )      *CHARP(ncp, i*2+1) = (signed char)val2;
    882                 else if ( size == 2 ) *SHORTP(ncp, i*2+2) = (short)val2;
    883                 else if ( size == 4 ) *LONGP(ncp, i*2+4) = (Py_Int32)val2;
    884         }
    885         return rv;
    886 }
    887 
    888 static PyObject *
    889 audioop_add(PyObject *self, PyObject *args)
    890 {
    891         signed char *cp1, *cp2, *ncp;
    892         int len1, len2, size, val1 = 0, val2 = 0, maxval, newval;
    893         PyObject *rv;
    894         int i;
    895 
    896         if ( !PyArg_ParseTuple(args, "s#s#i:add",
    897                           &cp1, &len1, &cp2, &len2, &size ) )
    898                 return 0;
    899 
    900         if ( len1 != len2 ) {
    901                 PyErr_SetString(AudioopError, "Lengths should be the same");
    902                 return 0;
    903         }
    904    
    905         if ( size == 1 ) maxval = 0x7f;
    906         else if ( size == 2 ) maxval = 0x7fff;
    907         else if ( size == 4 ) maxval = 0x7fffffff;
    908         else {
    909                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    910                 return 0;
    911         }
    912 
    913         rv = PyString_FromStringAndSize(NULL, len1);
    914         if ( rv == 0 )
    915                 return 0;
    916         ncp = (signed char *)PyString_AsString(rv);
    917 
    918         for ( i=0; i < len1; i += size ) {
    919                 if ( size == 1 )      val1 = (int)*CHARP(cp1, i);
    920                 else if ( size == 2 ) val1 = (int)*SHORTP(cp1, i);
    921                 else if ( size == 4 ) val1 = (int)*LONGP(cp1, i);
    922        
    923                 if ( size == 1 )      val2 = (int)*CHARP(cp2, i);
    924                 else if ( size == 2 ) val2 = (int)*SHORTP(cp2, i);
    925                 else if ( size == 4 ) val2 = (int)*LONGP(cp2, i);
    926 
    927                 newval = val1 + val2;
    928                 /* truncate in case of overflow */
    929                 if (newval > maxval) newval = maxval;
    930                 else if (newval < -maxval) newval = -maxval;
    931                 else if (size == 4 && (newval^val1) < 0 && (newval^val2) < 0)
    932                         newval = val1 > 0 ? maxval : - maxval;
    933 
    934                 if ( size == 1 )      *CHARP(ncp, i) = (signed char)newval;
    935                 else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
    936                 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
    937         }
    938         return rv;
     949
     950        if ( size == 1 )      *CHARP(ncp, i) = (signed char)newval;
     951        else if ( size == 2 ) *SHORTP(ncp, i) = (short)newval;
     952        else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)newval;
     953    }
     954    return rv;
    939955}
    940956
     
    942958audioop_bias(PyObject *self, PyObject *args)
    943959{
    944         signed char *cp, *ncp;
    945         int len, size, val = 0;
    946         PyObject *rv;
    947         int i;
    948         int bias;
    949 
    950         if ( !PyArg_ParseTuple(args, "s#ii:bias",
    951                           &cp, &len, &size , &bias) )
    952                 return 0;
    953 
    954         if ( size != 1 && size != 2 && size != 4) {
    955                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    956                 return 0;
    957         }
    958    
    959         rv = PyString_FromStringAndSize(NULL, len);
    960         if ( rv == 0 )
    961                 return 0;
    962         ncp = (signed char *)PyString_AsString(rv);
    963    
    964    
    965         for ( i=0; i < len; i += size ) {
    966                 if ( size == 1 )      val = (int)*CHARP(cp, i);
    967                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    968                 else if ( size == 4 ) val = (int)*LONGP(cp, i);
    969        
    970                 if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val+bias);
    971                 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val+bias);
    972                 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val+bias);
    973         }
    974         return rv;
     960    signed char *cp, *ncp;
     961    int len, size;
     962    unsigned int val = 0, mask;
     963    PyObject *rv;
     964    int i;
     965    int bias;
     966
     967    if ( !PyArg_ParseTuple(args, "s#ii:bias",
     968                      &cp, &len, &size , &bias) )
     969        return 0;
     970
     971    if (!audioop_check_parameters(len, size))
     972        return NULL;
     973
     974    rv = PyString_FromStringAndSize(NULL, len);
     975    if ( rv == 0 )
     976        return 0;
     977    ncp = (signed char *)PyString_AsString(rv);
     978
     979    mask = masks[size];
     980
     981    for ( i=0; i < len; i += size ) {
     982        if ( size == 1 )      val = (unsigned int)(unsigned char)*CHARP(cp, i);
     983        else if ( size == 2 ) val = (unsigned int)(unsigned short)*SHORTP(cp, i);
     984        else if ( size == 4 ) val = (unsigned int)(Py_UInt32)*LONGP(cp, i);
     985
     986        val += (unsigned int)bias;
     987        /* wrap around in case of overflow */
     988        val &= mask;
     989
     990        if ( size == 1 )      *CHARP(ncp, i) = (signed char)(unsigned char)val;
     991        else if ( size == 2 ) *SHORTP(ncp, i) = (short)(unsigned short)val;
     992        else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(Py_UInt32)val;
     993    }
     994    return rv;
    975995}
    976996
     
    978998audioop_reverse(PyObject *self, PyObject *args)
    979999{
    980         signed char *cp;
    981         unsigned char *ncp;
    982         int len, size, val = 0;
    983         PyObject *rv;
    984         int i, j;
    985 
    986         if ( !PyArg_ParseTuple(args, "s#i:reverse",
    987                           &cp, &len, &size) )
    988                 return 0;
    989 
    990         if ( size != 1 && size != 2 && size != 4 ) {
    991                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    992                 return 0;
    993         }
    994    
    995         rv = PyString_FromStringAndSize(NULL, len);
    996         if ( rv == 0 )
    997                 return 0;
    998         ncp = (unsigned char *)PyString_AsString(rv);
    999    
    1000         for ( i=0; i < len; i += size ) {
    1001                 if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
    1002                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    1003                 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
    1004 
    1005                 j = len - i - size;
    1006        
    1007                 if ( size == 1 )      *CHARP(ncp, j) = (signed char)(val >> 8);
    1008                 else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val);
    1009                 else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
    1010         }
    1011         return rv;
     1000    signed char *cp;
     1001    unsigned char *ncp;
     1002    int len, size, val = 0;
     1003    PyObject *rv;
     1004    int i, j;
     1005
     1006    if ( !PyArg_ParseTuple(args, "s#i:reverse",
     1007                      &cp, &len, &size) )
     1008        return 0;
     1009
     1010    if (!audioop_check_parameters(len, size))
     1011        return NULL;
     1012
     1013    rv = PyString_FromStringAndSize(NULL, len);
     1014    if ( rv == 0 )
     1015        return 0;
     1016    ncp = (unsigned char *)PyString_AsString(rv);
     1017
     1018    for ( i=0; i < len; i += size ) {
     1019        if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 24;
     1020        else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) << 16;
     1021        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     1022
     1023        j = len - i - size;
     1024
     1025        if ( size == 1 )      *CHARP(ncp, j) = (signed char)(val >> 24);
     1026        else if ( size == 2 ) *SHORTP(ncp, j) = (short)(val >> 16);
     1027        else if ( size == 4 ) *LONGP(ncp, j) = (Py_Int32)val;
     1028    }
     1029    return rv;
    10121030}
    10131031
     
    10151033audioop_lin2lin(PyObject *self, PyObject *args)
    10161034{
    1017         signed char *cp;
    1018         unsigned char *ncp;
    1019         int len, new_len, size, size2, val = 0;
    1020         PyObject *rv;
    1021         int i, j;
    1022 
    1023         if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
    1024                           &cp, &len, &size, &size2) )
    1025                 return 0;
    1026 
    1027         if ( (size != 1 && size != 2 && size != 4) ||
    1028              (size2 != 1 && size2 != 2 && size2 != 4)) {
    1029                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1030                 return 0;
    1031         }
    1032    
    1033         new_len = (len/size)*size2;
    1034         if (new_len < 0) {
    1035                 PyErr_SetString(PyExc_MemoryError,
    1036                                 "not enough memory for output buffer");
    1037                 return 0;
    1038         }
    1039         rv = PyString_FromStringAndSize(NULL, new_len);
    1040         if ( rv == 0 )
    1041                 return 0;
    1042         ncp = (unsigned char *)PyString_AsString(rv);
    1043    
    1044         for ( i=0, j=0; i < len; i += size, j += size2 ) {
    1045                 if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
    1046                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    1047                 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
    1048 
    1049                 if ( size2 == 1 )  *CHARP(ncp, j) = (signed char)(val >> 8);
    1050                 else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val);
    1051                 else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)(val<<16);
    1052         }
    1053         return rv;
     1035    signed char *cp;
     1036    unsigned char *ncp;
     1037    int len, size, size2, val = 0;
     1038    PyObject *rv;
     1039    int i, j;
     1040
     1041    if ( !PyArg_ParseTuple(args, "s#ii:lin2lin",
     1042                      &cp, &len, &size, &size2) )
     1043        return 0;
     1044
     1045    if (!audioop_check_parameters(len, size))
     1046        return NULL;
     1047    if (!audioop_check_size(size2))
     1048        return NULL;
     1049
     1050    if (len/size > INT_MAX/size2) {
     1051        PyErr_SetString(PyExc_MemoryError,
     1052                        "not enough memory for output buffer");
     1053        return 0;
     1054    }
     1055    rv = PyString_FromStringAndSize(NULL, (len/size)*size2);
     1056    if ( rv == 0 )
     1057        return 0;
     1058    ncp = (unsigned char *)PyString_AsString(rv);
     1059
     1060    for ( i=0, j=0; i < len; i += size, j += size2 ) {
     1061        if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 24;
     1062        else if ( size == 2 ) val = ((int)*SHORTP(cp, i)) << 16;
     1063        else if ( size == 4 ) val = (int)*LONGP(cp, i);
     1064
     1065        if ( size2 == 1 )  *CHARP(ncp, j) = (signed char)(val >> 24);
     1066        else if ( size2 == 2 ) *SHORTP(ncp, j) = (short)(val >> 16);
     1067        else if ( size2 == 4 ) *LONGP(ncp, j) = (Py_Int32)val;
     1068    }
     1069    return rv;
    10541070}
    10551071
     
    10571073gcd(int a, int b)
    10581074{
    1059         while (b > 0) {
    1060                 int tmp = a % b;
    1061                 a = b;
    1062                 b = tmp;
     1075    while (b > 0) {
     1076        int tmp = a % b;
     1077        a = b;
     1078        b = tmp;
     1079    }
     1080    return a;
     1081}
     1082
     1083static PyObject *
     1084audioop_ratecv(PyObject *self, PyObject *args)
     1085{
     1086    char *cp, *ncp;
     1087    int len, size, nchannels, inrate, outrate, weightA, weightB;
     1088    int chan, d, *prev_i, *cur_i, cur_o;
     1089    PyObject *state, *samps, *str, *rv = NULL;
     1090    int bytes_per_frame;
     1091
     1092    weightA = 1;
     1093    weightB = 0;
     1094    if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
     1095                          &nchannels, &inrate, &outrate, &state,
     1096                          &weightA, &weightB))
     1097        return NULL;
     1098    if (!audioop_check_size(size))
     1099        return NULL;
     1100    if (nchannels < 1) {
     1101        PyErr_SetString(AudioopError, "# of channels should be >= 1");
     1102        return NULL;
     1103    }
     1104    bytes_per_frame = size * nchannels;
     1105    if (bytes_per_frame / nchannels != size) {
     1106        /* This overflow test is rigorously correct because
     1107           both multiplicands are >= 1.  Use the argument names
     1108           from the docs for the error msg. */
     1109        PyErr_SetString(PyExc_OverflowError,
     1110                        "width * nchannels too big for a C int");
     1111        return NULL;
     1112    }
     1113    if (weightA < 1 || weightB < 0) {
     1114        PyErr_SetString(AudioopError,
     1115            "weightA should be >= 1, weightB should be >= 0");
     1116        return NULL;
     1117    }
     1118    if (len % bytes_per_frame != 0) {
     1119        PyErr_SetString(AudioopError, "not a whole number of frames");
     1120        return NULL;
     1121    }
     1122    if (inrate <= 0 || outrate <= 0) {
     1123        PyErr_SetString(AudioopError, "sampling rate not > 0");
     1124        return NULL;
     1125    }
     1126    /* divide inrate and outrate by their greatest common divisor */
     1127    d = gcd(inrate, outrate);
     1128    inrate /= d;
     1129    outrate /= d;
     1130    /* divide weightA and weightB by their greatest common divisor */
     1131    d = gcd(weightA, weightB);
     1132    weightA /= d;
     1133    weightA /= d;
     1134
     1135    if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) {
     1136        PyErr_SetString(PyExc_MemoryError,
     1137                        "not enough memory for output buffer");
     1138        return 0;
     1139    }
     1140    prev_i = (int *) malloc(nchannels * sizeof(int));
     1141    cur_i = (int *) malloc(nchannels * sizeof(int));
     1142    if (prev_i == NULL || cur_i == NULL) {
     1143        (void) PyErr_NoMemory();
     1144        goto exit;
     1145    }
     1146
     1147    len /= bytes_per_frame; /* # of frames */
     1148
     1149    if (state == Py_None) {
     1150        d = -outrate;
     1151        for (chan = 0; chan < nchannels; chan++)
     1152            prev_i[chan] = cur_i[chan] = 0;
     1153    }
     1154    else {
     1155        if (!PyArg_ParseTuple(state,
     1156                        "iO!;audioop.ratecv: illegal state argument",
     1157                        &d, &PyTuple_Type, &samps))
     1158            goto exit;
     1159        if (PyTuple_Size(samps) != nchannels) {
     1160            PyErr_SetString(AudioopError,
     1161                            "illegal state argument");
     1162            goto exit;
    10631163        }
    1064         return a;
    1065 }
    1066 
    1067 static PyObject *
    1068 audioop_ratecv(PyObject *self, PyObject *args)
    1069 {
    1070         char *cp, *ncp;
    1071         int len, size, nchannels, inrate, outrate, weightA, weightB;
    1072         int chan, d, *prev_i, *cur_i, cur_o;
    1073         PyObject *state, *samps, *str, *rv = NULL;
    1074         int bytes_per_frame;
    1075         size_t alloc_size;
    1076 
    1077         weightA = 1;
    1078         weightB = 0;
    1079         if (!PyArg_ParseTuple(args, "s#iiiiO|ii:ratecv", &cp, &len, &size,
    1080                               &nchannels, &inrate, &outrate, &state,
    1081                               &weightA, &weightB))
    1082                 return NULL;
    1083         if (size != 1 && size != 2 && size != 4) {
    1084                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1085                 return NULL;
    1086         }
    1087         if (nchannels < 1) {
    1088                 PyErr_SetString(AudioopError, "# of channels should be >= 1");
    1089                 return NULL;
    1090         }
    1091         bytes_per_frame = size * nchannels;
    1092         if (bytes_per_frame / nchannels != size) {
    1093                 /* This overflow test is rigorously correct because
    1094                    both multiplicands are >= 1.  Use the argument names
    1095                    from the docs for the error msg. */
    1096                 PyErr_SetString(PyExc_OverflowError,
    1097                                 "width * nchannels too big for a C int");
    1098                 return NULL;
    1099         }
    1100         if (weightA < 1 || weightB < 0) {
    1101                 PyErr_SetString(AudioopError,
    1102                         "weightA should be >= 1, weightB should be >= 0");
    1103                 return NULL;
    1104         }
    1105         if (len % bytes_per_frame != 0) {
    1106                 PyErr_SetString(AudioopError, "not a whole number of frames");
    1107                 return NULL;
    1108         }
    1109         if (inrate <= 0 || outrate <= 0) {
    1110                 PyErr_SetString(AudioopError, "sampling rate not > 0");
    1111                 return NULL;
    1112         }
    1113         /* divide inrate and outrate by their greatest common divisor */
    1114         d = gcd(inrate, outrate);
    1115         inrate /= d;
    1116         outrate /= d;
    1117 
    1118         alloc_size = sizeof(int) * (unsigned)nchannels;
    1119         if (alloc_size < nchannels) {
    1120                 PyErr_SetString(PyExc_MemoryError,
    1121                                 "not enough memory for output buffer");
    1122                 return 0;
    1123         }
    1124         prev_i = (int *) malloc(alloc_size);
    1125         cur_i = (int *) malloc(alloc_size);
    1126         if (prev_i == NULL || cur_i == NULL) {
    1127                 (void) PyErr_NoMemory();
     1164        for (chan = 0; chan < nchannels; chan++) {
     1165            if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
     1166                                  "ii:ratecv", &prev_i[chan],
     1167                                               &cur_i[chan]))
    11281168                goto exit;
    11291169        }
    1130 
    1131         len /= bytes_per_frame; /* # of frames */
    1132 
    1133         if (state == Py_None) {
    1134                 d = -outrate;
     1170    }
     1171
     1172    /* str <- Space for the output buffer. */
     1173    if (len == 0)
     1174        str = PyString_FromStringAndSize(NULL, 0);
     1175    else {
     1176        /* There are len input frames, so we need (mathematically)
     1177           ceiling(len*outrate/inrate) output frames, and each frame
     1178           requires bytes_per_frame bytes.  Computing this
     1179           without spurious overflow is the challenge; we can
     1180           settle for a reasonable upper bound, though, in this
     1181           case ceiling(len/inrate) * outrate. */
     1182
     1183        /* compute ceiling(len/inrate) without overflow */
     1184        int q = len > 0 ? 1 + (len - 1) / inrate : 0;
     1185        if (outrate > INT_MAX / q / bytes_per_frame)
     1186            str = NULL;
     1187        else
     1188            str = PyString_FromStringAndSize(NULL,
     1189                                             q * outrate * bytes_per_frame);
     1190    }
     1191    if (str == NULL) {
     1192        PyErr_SetString(PyExc_MemoryError,
     1193            "not enough memory for output buffer");
     1194        goto exit;
     1195    }
     1196    ncp = PyString_AsString(str);
     1197
     1198    for (;;) {
     1199        while (d < 0) {
     1200            if (len == 0) {
     1201                samps = PyTuple_New(nchannels);
     1202                if (samps == NULL)
     1203                    goto exit;
    11351204                for (chan = 0; chan < nchannels; chan++)
    1136                         prev_i[chan] = cur_i[chan] = 0;
     1205                    PyTuple_SetItem(samps, chan,
     1206                        Py_BuildValue("(ii)",
     1207                                      prev_i[chan],
     1208                                      cur_i[chan]));
     1209                if (PyErr_Occurred())
     1210                    goto exit;
     1211                /* We have checked before that the length
     1212                 * of the string fits into int. */
     1213                len = (int)(ncp - PyString_AsString(str));
     1214                if (len == 0) {
     1215                    /*don't want to resize to zero length*/
     1216                    rv = PyString_FromStringAndSize("", 0);
     1217                    Py_DECREF(str);
     1218                    str = rv;
     1219                } else if (_PyString_Resize(&str, len) < 0)
     1220                    goto exit;
     1221                rv = Py_BuildValue("(O(iO))", str, d, samps);
     1222                Py_DECREF(samps);
     1223                Py_DECREF(str);
     1224                goto exit; /* return rv */
     1225            }
     1226            for (chan = 0; chan < nchannels; chan++) {
     1227                prev_i[chan] = cur_i[chan];
     1228                if (size == 1)
     1229                    cur_i[chan] = ((int)*CHARP(cp, 0)) << 24;
     1230                else if (size == 2)
     1231                    cur_i[chan] = ((int)*SHORTP(cp, 0)) << 16;
     1232                else if (size == 4)
     1233                    cur_i[chan] = (int)*LONGP(cp, 0);
     1234                cp += size;
     1235                /* implements a simple digital filter */
     1236                cur_i[chan] = (int)(
     1237                    ((double)weightA * (double)cur_i[chan] +
     1238                     (double)weightB * (double)prev_i[chan]) /
     1239                    ((double)weightA + (double)weightB));
     1240            }
     1241            len--;
     1242            d += outrate;
    11371243        }
    1138         else {
    1139                 if (!PyArg_ParseTuple(state,
    1140                                 "iO!;audioop.ratecv: illegal state argument",
    1141                                 &d, &PyTuple_Type, &samps))
    1142                         goto exit;
    1143                 if (PyTuple_Size(samps) != nchannels) {
    1144                         PyErr_SetString(AudioopError,
    1145                                         "illegal state argument");
    1146                         goto exit;
    1147                 }
    1148                 for (chan = 0; chan < nchannels; chan++) {
    1149                         if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
    1150                                               "ii:ratecv", &prev_i[chan],
    1151                                                            &cur_i[chan]))
    1152                                 goto exit;
    1153                 }
     1244        while (d >= 0) {
     1245            for (chan = 0; chan < nchannels; chan++) {
     1246                cur_o = (int)(((double)prev_i[chan] * (double)d +
     1247                         (double)cur_i[chan] * (double)(outrate - d)) /
     1248                    (double)outrate);
     1249                if (size == 1)
     1250                    *CHARP(ncp, 0) = (signed char)(cur_o >> 24);
     1251                else if (size == 2)
     1252                    *SHORTP(ncp, 0) = (short)(cur_o >> 16);
     1253                else if (size == 4)
     1254                    *LONGP(ncp, 0) = (Py_Int32)(cur_o);
     1255                ncp += size;
     1256            }
     1257            d -= inrate;
    11541258        }
    1155 
    1156         /* str <- Space for the output buffer. */
    1157         {
    1158                 /* There are len input frames, so we need (mathematically)
    1159                    ceiling(len*outrate/inrate) output frames, and each frame
    1160                    requires bytes_per_frame bytes.  Computing this
    1161                    without spurious overflow is the challenge; we can
    1162                    settle for a reasonable upper bound, though. */
    1163                 int ceiling;   /* the number of output frames */
    1164                 int nbytes;    /* the number of output bytes needed */
    1165                 int q = len / inrate;
    1166                 /* Now len = q * inrate + r exactly (with r = len % inrate),
    1167                    and this is less than q * inrate + inrate = (q+1)*inrate.
    1168                    So a reasonable upper bound on len*outrate/inrate is
    1169                    ((q+1)*inrate)*outrate/inrate =
    1170                    (q+1)*outrate.
    1171                 */
    1172                 ceiling = (q+1) * outrate;
    1173                 nbytes = ceiling * bytes_per_frame;
    1174                 /* See whether anything overflowed; if not, get the space. */
    1175                 if (q+1 < 0 ||
    1176                     ceiling / outrate != q+1 ||
    1177                     nbytes / bytes_per_frame != ceiling)
    1178                         str = NULL;
    1179                 else
    1180                         str = PyString_FromStringAndSize(NULL, nbytes);
    1181 
    1182                 if (str == NULL) {
    1183                         PyErr_SetString(PyExc_MemoryError,
    1184                                 "not enough memory for output buffer");
    1185                         goto exit;
    1186                 }
     1259    }
     1260  exit:
     1261    if (prev_i != NULL)
     1262        free(prev_i);
     1263    if (cur_i != NULL)
     1264        free(cur_i);
     1265    return rv;
     1266}
     1267
     1268static PyObject *
     1269audioop_lin2ulaw(PyObject *self, PyObject *args)
     1270{
     1271    signed char *cp;
     1272    unsigned char *ncp;
     1273    int len, size, val = 0;
     1274    PyObject *rv;
     1275    int i;
     1276
     1277    if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
     1278                           &cp, &len, &size) )
     1279        return 0 ;
     1280
     1281    if (!audioop_check_parameters(len, size))
     1282        return NULL;
     1283
     1284    rv = PyString_FromStringAndSize(NULL, len/size);
     1285    if ( rv == 0 )
     1286        return 0;
     1287    ncp = (unsigned char *)PyString_AsString(rv);
     1288
     1289    for ( i=0; i < len; i += size ) {
     1290        if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
     1291        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     1292        else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
     1293
     1294        *ncp++ = st_14linear2ulaw(val);
     1295    }
     1296    return rv;
     1297}
     1298
     1299static PyObject *
     1300audioop_ulaw2lin(PyObject *self, PyObject *args)
     1301{
     1302    unsigned char *cp;
     1303    unsigned char cval;
     1304    signed char *ncp;
     1305    int len, size, val;
     1306    PyObject *rv;
     1307    int i;
     1308
     1309    if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
     1310                           &cp, &len, &size) )
     1311        return 0;
     1312
     1313    if (!audioop_check_size(size))
     1314        return NULL;
     1315
     1316    if (len > INT_MAX/size) {
     1317        PyErr_SetString(PyExc_MemoryError,
     1318                        "not enough memory for output buffer");
     1319        return 0;
     1320    }
     1321    rv = PyString_FromStringAndSize(NULL, len*size);
     1322    if ( rv == 0 )
     1323        return 0;
     1324    ncp = (signed char *)PyString_AsString(rv);
     1325
     1326    for ( i=0; i < len*size; i += size ) {
     1327        cval = *cp++;
     1328        val = st_ulaw2linear16(cval);
     1329
     1330        if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
     1331        else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
     1332        else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
     1333    }
     1334    return rv;
     1335}
     1336
     1337static PyObject *
     1338audioop_lin2alaw(PyObject *self, PyObject *args)
     1339{
     1340    signed char *cp;
     1341    unsigned char *ncp;
     1342    int len, size, val = 0;
     1343    PyObject *rv;
     1344    int i;
     1345
     1346    if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
     1347                           &cp, &len, &size) )
     1348        return 0;
     1349
     1350    if (!audioop_check_parameters(len, size))
     1351        return NULL;
     1352
     1353    rv = PyString_FromStringAndSize(NULL, len/size);
     1354    if ( rv == 0 )
     1355        return 0;
     1356    ncp = (unsigned char *)PyString_AsString(rv);
     1357
     1358    for ( i=0; i < len; i += size ) {
     1359        if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
     1360        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     1361        else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
     1362
     1363        *ncp++ = st_linear2alaw(val);
     1364    }
     1365    return rv;
     1366}
     1367
     1368static PyObject *
     1369audioop_alaw2lin(PyObject *self, PyObject *args)
     1370{
     1371    unsigned char *cp;
     1372    unsigned char cval;
     1373    signed char *ncp;
     1374    int len, size, val;
     1375    PyObject *rv;
     1376    int i;
     1377
     1378    if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
     1379                           &cp, &len, &size) )
     1380        return 0;
     1381
     1382    if (!audioop_check_size(size))
     1383        return NULL;
     1384
     1385    if (len > INT_MAX/size) {
     1386        PyErr_SetString(PyExc_MemoryError,
     1387                        "not enough memory for output buffer");
     1388        return 0;
     1389    }
     1390    rv = PyString_FromStringAndSize(NULL, len*size);
     1391    if ( rv == 0 )
     1392        return 0;
     1393    ncp = (signed char *)PyString_AsString(rv);
     1394
     1395    for ( i=0; i < len*size; i += size ) {
     1396        cval = *cp++;
     1397        val = st_alaw2linear16(cval);
     1398
     1399        if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
     1400        else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
     1401        else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
     1402    }
     1403    return rv;
     1404}
     1405
     1406static PyObject *
     1407audioop_lin2adpcm(PyObject *self, PyObject *args)
     1408{
     1409    signed char *cp;
     1410    signed char *ncp;
     1411    int len, size, val = 0, step, valpred, delta,
     1412        index, sign, vpdiff, diff;
     1413    PyObject *rv, *state, *str;
     1414    int i, outputbuffer = 0, bufferstep;
     1415
     1416    if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
     1417                           &cp, &len, &size, &state) )
     1418        return 0;
     1419
     1420    if (!audioop_check_parameters(len, size))
     1421        return NULL;
     1422
     1423    str = PyString_FromStringAndSize(NULL, len/(size*2));
     1424    if ( str == 0 )
     1425        return 0;
     1426    ncp = (signed char *)PyString_AsString(str);
     1427
     1428    /* Decode state, should have (value, step) */
     1429    if ( state == Py_None ) {
     1430        /* First time, it seems. Set defaults */
     1431        valpred = 0;
     1432        index = 0;
     1433    } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
     1434        return 0;
     1435
     1436    step = stepsizeTable[index];
     1437    bufferstep = 1;
     1438
     1439    for ( i=0; i < len; i += size ) {
     1440        if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
     1441        else if ( size == 2 ) val = (int)*SHORTP(cp, i);
     1442        else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
     1443
     1444        /* Step 1 - compute difference with previous value */
     1445        diff = val - valpred;
     1446        sign = (diff < 0) ? 8 : 0;
     1447        if ( sign ) diff = (-diff);
     1448
     1449        /* Step 2 - Divide and clamp */
     1450        /* Note:
     1451        ** This code *approximately* computes:
     1452        **    delta = diff*4/step;
     1453        **    vpdiff = (delta+0.5)*step/4;
     1454        ** but in shift step bits are dropped. The net result of this
     1455        ** is that even if you have fast mul/div hardware you cannot
     1456        ** put it to good use since the fixup would be too expensive.
     1457        */
     1458        delta = 0;
     1459        vpdiff = (step >> 3);
     1460
     1461        if ( diff >= step ) {
     1462            delta = 4;
     1463            diff -= step;
     1464            vpdiff += step;
    11871465        }
    1188         ncp = PyString_AsString(str);
    1189 
    1190         for (;;) {
    1191                 while (d < 0) {
    1192                         if (len == 0) {
    1193                                 samps = PyTuple_New(nchannels);
    1194                                 if (samps == NULL)
    1195                                         goto exit;
    1196                                 for (chan = 0; chan < nchannels; chan++)
    1197                                         PyTuple_SetItem(samps, chan,
    1198                                                 Py_BuildValue("(ii)",
    1199                                                               prev_i[chan],
    1200                                                               cur_i[chan]));
    1201                                 if (PyErr_Occurred())
    1202                                         goto exit;
    1203                                 /* We have checked before that the length
    1204                                  * of the string fits into int. */
    1205                                 len = (int)(ncp - PyString_AsString(str));
    1206                                 if (len == 0) {
    1207                                         /*don't want to resize to zero length*/
    1208                                         rv = PyString_FromStringAndSize("", 0);
    1209                                         Py_DECREF(str);
    1210                                         str = rv;
    1211                                 } else if (_PyString_Resize(&str, len) < 0)
    1212                                         goto exit;
    1213                                 rv = Py_BuildValue("(O(iO))", str, d, samps);
    1214                                 Py_DECREF(samps);
    1215                                 Py_DECREF(str);
    1216                                 goto exit; /* return rv */
    1217                         }
    1218                         for (chan = 0; chan < nchannels; chan++) {
    1219                                 prev_i[chan] = cur_i[chan];
    1220                                 if (size == 1)
    1221                                     cur_i[chan] = ((int)*CHARP(cp, 0)) << 8;
    1222                                 else if (size == 2)
    1223                                     cur_i[chan] = (int)*SHORTP(cp, 0);
    1224                                 else if (size == 4)
    1225                                     cur_i[chan] = ((int)*LONGP(cp, 0)) >> 16;
    1226                                 cp += size;
    1227                                 /* implements a simple digital filter */
    1228                                 cur_i[chan] =
    1229                                         (weightA * cur_i[chan] +
    1230                                          weightB * prev_i[chan]) /
    1231                                         (weightA + weightB);
    1232                         }
    1233                         len--;
    1234                         d += outrate;
    1235                 }
    1236                 while (d >= 0) {
    1237                         for (chan = 0; chan < nchannels; chan++) {
    1238                                 cur_o = (prev_i[chan] * d +
    1239                                          cur_i[chan] * (outrate - d)) /
    1240                                         outrate;
    1241                                 if (size == 1)
    1242                                     *CHARP(ncp, 0) = (signed char)(cur_o >> 8);
    1243                                 else if (size == 2)
    1244                                     *SHORTP(ncp, 0) = (short)(cur_o);
    1245                                 else if (size == 4)
    1246                                     *LONGP(ncp, 0) = (Py_Int32)(cur_o<<16);
    1247                                 ncp += size;
    1248                         }
    1249                         d -= inrate;
    1250                 }
     1466        step >>= 1;
     1467        if ( diff >= step  ) {
     1468            delta |= 2;
     1469            diff -= step;
     1470            vpdiff += step;
    12511471        }
    1252   exit:
    1253         if (prev_i != NULL)
    1254                 free(prev_i);
    1255         if (cur_i != NULL)
    1256                 free(cur_i);
    1257         return rv;
    1258 }
    1259 
    1260 static PyObject *
    1261 audioop_lin2ulaw(PyObject *self, PyObject *args)
    1262 {
    1263         signed char *cp;
    1264         unsigned char *ncp;
    1265         int len, size, val = 0;
    1266         PyObject *rv;
    1267         int i;
    1268 
    1269         if ( !PyArg_ParseTuple(args, "s#i:lin2ulaw",
    1270                                &cp, &len, &size) )
    1271                 return 0 ;
    1272 
    1273         if ( size != 1 && size != 2 && size != 4) {
    1274                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1275                 return 0;
     1472        step >>= 1;
     1473        if ( diff >= step ) {
     1474            delta |= 1;
     1475            vpdiff += step;
    12761476        }
    1277    
    1278         rv = PyString_FromStringAndSize(NULL, len/size);
    1279         if ( rv == 0 )
    1280                 return 0;
    1281         ncp = (unsigned char *)PyString_AsString(rv);
    1282    
    1283         for ( i=0; i < len; i += size ) {
    1284                 if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
    1285                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    1286                 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
    1287 
    1288                 *ncp++ = st_14linear2ulaw(val);
     1477
     1478        /* Step 3 - Update previous value */
     1479        if ( sign )
     1480            valpred -= vpdiff;
     1481        else
     1482            valpred += vpdiff;
     1483
     1484        /* Step 4 - Clamp previous value to 16 bits */
     1485        if ( valpred > 32767 )
     1486            valpred = 32767;
     1487        else if ( valpred < -32768 )
     1488            valpred = -32768;
     1489
     1490        /* Step 5 - Assemble value, update index and step values */
     1491        delta |= sign;
     1492
     1493        index += indexTable[delta];
     1494        if ( index < 0 ) index = 0;
     1495        if ( index > 88 ) index = 88;
     1496        step = stepsizeTable[index];
     1497
     1498        /* Step 6 - Output value */
     1499        if ( bufferstep ) {
     1500            outputbuffer = (delta << 4) & 0xf0;
     1501        } else {
     1502            *ncp++ = (delta & 0x0f) | outputbuffer;
    12891503        }
    1290         return rv;
    1291 }
    1292 
    1293 static PyObject *
    1294 audioop_ulaw2lin(PyObject *self, PyObject *args)
    1295 {
    1296         unsigned char *cp;
    1297         unsigned char cval;
    1298         signed char *ncp;
    1299         int len, new_len, size, val;
    1300         PyObject *rv;
    1301         int i;
    1302 
    1303         if ( !PyArg_ParseTuple(args, "s#i:ulaw2lin",
    1304                                &cp, &len, &size) )
    1305                 return 0;
    1306 
    1307         if ( size != 1 && size != 2 && size != 4) {
    1308                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1309                 return 0;
     1504        bufferstep = !bufferstep;
     1505    }
     1506    rv = Py_BuildValue("(O(ii))", str, valpred, index);
     1507    Py_DECREF(str);
     1508    return rv;
     1509}
     1510
     1511static PyObject *
     1512audioop_adpcm2lin(PyObject *self, PyObject *args)
     1513{
     1514    signed char *cp;
     1515    signed char *ncp;
     1516    int len, size, valpred, step, delta, index, sign, vpdiff;
     1517    PyObject *rv, *str, *state;
     1518    int i, inputbuffer = 0, bufferstep;
     1519
     1520    if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
     1521                           &cp, &len, &size, &state) )
     1522        return 0;
     1523
     1524    if (!audioop_check_size(size))
     1525        return NULL;
     1526
     1527    /* Decode state, should have (value, step) */
     1528    if ( state == Py_None ) {
     1529        /* First time, it seems. Set defaults */
     1530        valpred = 0;
     1531        index = 0;
     1532    } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
     1533        return 0;
     1534
     1535    if (len > (INT_MAX/2)/size) {
     1536        PyErr_SetString(PyExc_MemoryError,
     1537                        "not enough memory for output buffer");
     1538        return 0;
     1539    }
     1540    str = PyString_FromStringAndSize(NULL, len*size*2);
     1541    if ( str == 0 )
     1542        return 0;
     1543    ncp = (signed char *)PyString_AsString(str);
     1544
     1545    step = stepsizeTable[index];
     1546    bufferstep = 0;
     1547
     1548    for ( i=0; i < len*size*2; i += size ) {
     1549        /* Step 1 - get the delta value and compute next index */
     1550        if ( bufferstep ) {
     1551            delta = inputbuffer & 0xf;
     1552        } else {
     1553            inputbuffer = *cp++;
     1554            delta = (inputbuffer >> 4) & 0xf;
    13101555        }
    1311    
    1312         new_len = len*size;
    1313         if (new_len < 0) {
    1314                 PyErr_SetString(PyExc_MemoryError,
    1315                                 "not enough memory for output buffer");
    1316                 return 0;
    1317         }
    1318         rv = PyString_FromStringAndSize(NULL, new_len);
    1319         if ( rv == 0 )
    1320                 return 0;
    1321         ncp = (signed char *)PyString_AsString(rv);
    1322    
    1323         for ( i=0; i < new_len; i += size ) {
    1324                 cval = *cp++;
    1325                 val = st_ulaw2linear16(cval);
    1326        
    1327                 if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
    1328                 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
    1329                 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
    1330         }
    1331         return rv;
    1332 }
    1333 
    1334 static PyObject *
    1335 audioop_lin2alaw(PyObject *self, PyObject *args)
    1336 {
    1337         signed char *cp;
    1338         unsigned char *ncp;
    1339         int len, size, val = 0;
    1340         PyObject *rv;
    1341         int i;
    1342 
    1343         if ( !PyArg_ParseTuple(args, "s#i:lin2alaw",
    1344                                &cp, &len, &size) )
    1345                 return 0;
    1346 
    1347         if ( size != 1 && size != 2 && size != 4) {
    1348                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1349                 return 0;
    1350         }
    1351    
    1352         rv = PyString_FromStringAndSize(NULL, len/size);
    1353         if ( rv == 0 )
    1354                 return 0;
    1355         ncp = (unsigned char *)PyString_AsString(rv);
    1356    
    1357         for ( i=0; i < len; i += size ) {
    1358                 if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
    1359                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    1360                 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
    1361 
    1362                 *ncp++ = st_linear2alaw(val);
    1363         }
    1364         return rv;
    1365 }
    1366 
    1367 static PyObject *
    1368 audioop_alaw2lin(PyObject *self, PyObject *args)
    1369 {
    1370         unsigned char *cp;
    1371         unsigned char cval;
    1372         signed char *ncp;
    1373         int len, new_len, size, val;
    1374         PyObject *rv;
    1375         int i;
    1376 
    1377         if ( !PyArg_ParseTuple(args, "s#i:alaw2lin",
    1378                                &cp, &len, &size) )
    1379                 return 0;
    1380 
    1381         if ( size != 1 && size != 2 && size != 4) {
    1382                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1383                 return 0;
    1384         }
    1385    
    1386         new_len = len*size;
    1387         if (new_len < 0) {
    1388                 PyErr_SetString(PyExc_MemoryError,
    1389                                 "not enough memory for output buffer");
    1390                 return 0;
    1391         }
    1392         rv = PyString_FromStringAndSize(NULL, new_len);
    1393         if ( rv == 0 )
    1394                 return 0;
    1395         ncp = (signed char *)PyString_AsString(rv);
    1396    
    1397         for ( i=0; i < new_len; i += size ) {
    1398                 cval = *cp++;
    1399                 val = st_alaw2linear16(cval);
    1400        
    1401                 if ( size == 1 )      *CHARP(ncp, i) = (signed char)(val >> 8);
    1402                 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(val);
    1403                 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(val<<16);
    1404         }
    1405         return rv;
    1406 }
    1407 
    1408 static PyObject *
    1409 audioop_lin2adpcm(PyObject *self, PyObject *args)
    1410 {
    1411         signed char *cp;
    1412         signed char *ncp;
    1413         int len, size, val = 0, step, valpred, delta,
    1414                 index, sign, vpdiff, diff;
    1415         PyObject *rv, *state, *str;
    1416         int i, outputbuffer = 0, bufferstep;
    1417 
    1418         if ( !PyArg_ParseTuple(args, "s#iO:lin2adpcm",
    1419                                &cp, &len, &size, &state) )
    1420                 return 0;
    1421    
    1422 
    1423         if ( size != 1 && size != 2 && size != 4) {
    1424                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1425                 return 0;
    1426         }
    1427    
    1428         str = PyString_FromStringAndSize(NULL, len/(size*2));
    1429         if ( str == 0 )
    1430                 return 0;
    1431         ncp = (signed char *)PyString_AsString(str);
    1432 
    1433         /* Decode state, should have (value, step) */
    1434         if ( state == Py_None ) {
    1435                 /* First time, it seems. Set defaults */
    1436                 valpred = 0;
    1437                 step = 7;
    1438                 index = 0;
    1439         } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
    1440                 return 0;
    1441 
     1556
     1557        bufferstep = !bufferstep;
     1558
     1559        /* Step 2 - Find new index value (for later) */
     1560        index += indexTable[delta];
     1561        if ( index < 0 ) index = 0;
     1562        if ( index > 88 ) index = 88;
     1563
     1564        /* Step 3 - Separate sign and magnitude */
     1565        sign = delta & 8;
     1566        delta = delta & 7;
     1567
     1568        /* Step 4 - Compute difference and new predicted value */
     1569        /*
     1570        ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
     1571        ** in adpcm_coder.
     1572        */
     1573        vpdiff = step >> 3;
     1574        if ( delta & 4 ) vpdiff += step;
     1575        if ( delta & 2 ) vpdiff += step>>1;
     1576        if ( delta & 1 ) vpdiff += step>>2;
     1577
     1578        if ( sign )
     1579            valpred -= vpdiff;
     1580        else
     1581            valpred += vpdiff;
     1582
     1583        /* Step 5 - clamp output value */
     1584        if ( valpred > 32767 )
     1585            valpred = 32767;
     1586        else if ( valpred < -32768 )
     1587            valpred = -32768;
     1588
     1589        /* Step 6 - Update step value */
    14421590        step = stepsizeTable[index];
    1443         bufferstep = 1;
    1444 
    1445         for ( i=0; i < len; i += size ) {
    1446                 if ( size == 1 )      val = ((int)*CHARP(cp, i)) << 8;
    1447                 else if ( size == 2 ) val = (int)*SHORTP(cp, i);
    1448                 else if ( size == 4 ) val = ((int)*LONGP(cp, i)) >> 16;
    1449 
    1450                 /* Step 1 - compute difference with previous value */
    1451                 diff = val - valpred;
    1452                 sign = (diff < 0) ? 8 : 0;
    1453                 if ( sign ) diff = (-diff);
    1454 
    1455                 /* Step 2 - Divide and clamp */
    1456                 /* Note:
    1457                 ** This code *approximately* computes:
    1458                 **    delta = diff*4/step;
    1459                 **    vpdiff = (delta+0.5)*step/4;
    1460                 ** but in shift step bits are dropped. The net result of this
    1461                 ** is that even if you have fast mul/div hardware you cannot
    1462                 ** put it to good use since the fixup would be too expensive.
    1463                 */
    1464                 delta = 0;
    1465                 vpdiff = (step >> 3);
    1466        
    1467                 if ( diff >= step ) {
    1468                         delta = 4;
    1469                         diff -= step;
    1470                         vpdiff += step;
    1471                 }
    1472                 step >>= 1;
    1473                 if ( diff >= step  ) {
    1474                         delta |= 2;
    1475                         diff -= step;
    1476                         vpdiff += step;
    1477                 }
    1478                 step >>= 1;
    1479                 if ( diff >= step ) {
    1480                         delta |= 1;
    1481                         vpdiff += step;
    1482                 }
    1483 
    1484                 /* Step 3 - Update previous value */
    1485                 if ( sign )
    1486                         valpred -= vpdiff;
    1487                 else
    1488                         valpred += vpdiff;
    1489 
    1490                 /* Step 4 - Clamp previous value to 16 bits */
    1491                 if ( valpred > 32767 )
    1492                         valpred = 32767;
    1493                 else if ( valpred < -32768 )
    1494                         valpred = -32768;
    1495 
    1496                 /* Step 5 - Assemble value, update index and step values */
    1497                 delta |= sign;
    1498        
    1499                 index += indexTable[delta];
    1500                 if ( index < 0 ) index = 0;
    1501                 if ( index > 88 ) index = 88;
    1502                 step = stepsizeTable[index];
    1503 
    1504                 /* Step 6 - Output value */
    1505                 if ( bufferstep ) {
    1506                         outputbuffer = (delta << 4) & 0xf0;
    1507                 } else {
    1508                         *ncp++ = (delta & 0x0f) | outputbuffer;
    1509                 }
    1510                 bufferstep = !bufferstep;
    1511         }
    1512         rv = Py_BuildValue("(O(ii))", str, valpred, index);
    1513         Py_DECREF(str);
    1514         return rv;
    1515 }
    1516 
    1517 static PyObject *
    1518 audioop_adpcm2lin(PyObject *self, PyObject *args)
    1519 {
    1520         signed char *cp;
    1521         signed char *ncp;
    1522         int len, new_len, size, valpred, step, delta, index, sign, vpdiff;
    1523         PyObject *rv, *str, *state;
    1524         int i, inputbuffer = 0, bufferstep;
    1525 
    1526         if ( !PyArg_ParseTuple(args, "s#iO:adpcm2lin",
    1527                                &cp, &len, &size, &state) )
    1528                 return 0;
    1529 
    1530         if ( size != 1 && size != 2 && size != 4) {
    1531                 PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
    1532                 return 0;
    1533         }
    1534    
    1535         /* Decode state, should have (value, step) */
    1536         if ( state == Py_None ) {
    1537                 /* First time, it seems. Set defaults */
    1538                 valpred = 0;
    1539                 step = 7;
    1540                 index = 0;
    1541         } else if ( !PyArg_ParseTuple(state, "ii", &valpred, &index) )
    1542                 return 0;
    1543    
    1544         new_len = len*size*2;
    1545         if (new_len < 0) {
    1546                 PyErr_SetString(PyExc_MemoryError,
    1547                                 "not enough memory for output buffer");
    1548                 return 0;
    1549         }
    1550         str = PyString_FromStringAndSize(NULL, new_len);
    1551         if ( str == 0 )
    1552                 return 0;
    1553         ncp = (signed char *)PyString_AsString(str);
    1554 
    1555         step = stepsizeTable[index];
    1556         bufferstep = 0;
    1557    
    1558         for ( i=0; i < new_len; i += size ) {
    1559                 /* Step 1 - get the delta value and compute next index */
    1560                 if ( bufferstep ) {
    1561                         delta = inputbuffer & 0xf;
    1562                 } else {
    1563                         inputbuffer = *cp++;
    1564                         delta = (inputbuffer >> 4) & 0xf;
    1565                 }
    1566 
    1567                 bufferstep = !bufferstep;
    1568 
    1569                 /* Step 2 - Find new index value (for later) */
    1570                 index += indexTable[delta];
    1571                 if ( index < 0 ) index = 0;
    1572                 if ( index > 88 ) index = 88;
    1573 
    1574                 /* Step 3 - Separate sign and magnitude */
    1575                 sign = delta & 8;
    1576                 delta = delta & 7;
    1577 
    1578                 /* Step 4 - Compute difference and new predicted value */
    1579                 /*
    1580                 ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
    1581                 ** in adpcm_coder.
    1582                 */
    1583                 vpdiff = step >> 3;
    1584                 if ( delta & 4 ) vpdiff += step;
    1585                 if ( delta & 2 ) vpdiff += step>>1;
    1586                 if ( delta & 1 ) vpdiff += step>>2;
    1587 
    1588                 if ( sign )
    1589                         valpred -= vpdiff;
    1590                 else
    1591                         valpred += vpdiff;
    1592 
    1593                 /* Step 5 - clamp output value */
    1594                 if ( valpred > 32767 )
    1595                         valpred = 32767;
    1596                 else if ( valpred < -32768 )
    1597                         valpred = -32768;
    1598 
    1599                 /* Step 6 - Update step value */
    1600                 step = stepsizeTable[index];
    1601 
    1602                 /* Step 6 - Output value */
    1603                 if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
    1604                 else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
    1605                 else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
    1606         }
    1607 
    1608         rv = Py_BuildValue("(O(ii))", str, valpred, index);
    1609         Py_DECREF(str);
    1610         return rv;
     1591
     1592        /* Step 6 - Output value */
     1593        if ( size == 1 ) *CHARP(ncp, i) = (signed char)(valpred >> 8);
     1594        else if ( size == 2 ) *SHORTP(ncp, i) = (short)(valpred);
     1595        else if ( size == 4 ) *LONGP(ncp, i) = (Py_Int32)(valpred<<16);
     1596    }
     1597
     1598    rv = Py_BuildValue("(O(ii))", str, valpred, index);
     1599    Py_DECREF(str);
     1600    return rv;
    16111601}
    16121602
    16131603static PyMethodDef audioop_methods[] = {
    1614         { "max", audioop_max, METH_VARARGS },
    1615         { "minmax", audioop_minmax, METH_VARARGS },
    1616         { "avg", audioop_avg, METH_VARARGS },
    1617         { "maxpp", audioop_maxpp, METH_VARARGS },
    1618         { "avgpp", audioop_avgpp, METH_VARARGS },
    1619         { "rms", audioop_rms, METH_VARARGS },
    1620         { "findfit", audioop_findfit, METH_VARARGS },
    1621         { "findmax", audioop_findmax, METH_VARARGS },
    1622         { "findfactor", audioop_findfactor, METH_VARARGS },
    1623         { "cross", audioop_cross, METH_VARARGS },
    1624         { "mul", audioop_mul, METH_VARARGS },
    1625         { "add", audioop_add, METH_VARARGS },
    1626         { "bias", audioop_bias, METH_VARARGS },
    1627         { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
    1628         { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
    1629         { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
    1630         { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
    1631         { "lin2lin", audioop_lin2lin, METH_VARARGS },
    1632         { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
    1633         { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
    1634         { "tomono", audioop_tomono, METH_VARARGS },
    1635         { "tostereo", audioop_tostereo, METH_VARARGS },
    1636         { "getsample", audioop_getsample, METH_VARARGS },
    1637         { "reverse", audioop_reverse, METH_VARARGS },
    1638         { "ratecv", audioop_ratecv, METH_VARARGS },
    1639         { 0,          0 }
     1604    { "max", audioop_max, METH_VARARGS },
     1605    { "minmax", audioop_minmax, METH_VARARGS },
     1606    { "avg", audioop_avg, METH_VARARGS },
     1607    { "maxpp", audioop_maxpp, METH_VARARGS },
     1608    { "avgpp", audioop_avgpp, METH_VARARGS },
     1609    { "rms", audioop_rms, METH_VARARGS },
     1610    { "findfit", audioop_findfit, METH_VARARGS },
     1611    { "findmax", audioop_findmax, METH_VARARGS },
     1612    { "findfactor", audioop_findfactor, METH_VARARGS },
     1613    { "cross", audioop_cross, METH_VARARGS },
     1614    { "mul", audioop_mul, METH_VARARGS },
     1615    { "add", audioop_add, METH_VARARGS },
     1616    { "bias", audioop_bias, METH_VARARGS },
     1617    { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
     1618    { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
     1619    { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
     1620    { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
     1621    { "lin2lin", audioop_lin2lin, METH_VARARGS },
     1622    { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
     1623    { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
     1624    { "tomono", audioop_tomono, METH_VARARGS },
     1625    { "tostereo", audioop_tostereo, METH_VARARGS },
     1626    { "getsample", audioop_getsample, METH_VARARGS },
     1627    { "reverse", audioop_reverse, METH_VARARGS },
     1628    { "ratecv", audioop_ratecv, METH_VARARGS },
     1629    { 0,          0 }
    16401630};
    16411631
     
    16431633initaudioop(void)
    16441634{
    1645         PyObject *m, *d;
    1646         m = Py_InitModule("audioop", audioop_methods);
    1647         if (m == NULL)
    1648                 return;
    1649         d = PyModule_GetDict(m);
    1650         if (d == NULL)
    1651                 return;
    1652         AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
    1653         if (AudioopError != NULL)
    1654              PyDict_SetItemString(d,"error",AudioopError);
    1655 }
     1635    PyObject *m, *d;
     1636    m = Py_InitModule("audioop", audioop_methods);
     1637    if (m == NULL)
     1638        return;
     1639    d = PyModule_GetDict(m);
     1640    if (d == NULL)
     1641        return;
     1642    AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
     1643    if (AudioopError != NULL)
     1644         PyDict_SetItemString(d,"error",AudioopError);
     1645}
Note: See TracChangeset for help on using the changeset viewer.