~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Modules/audioop.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* audioopmodule - Module to detect peak values in arrays */
 
3
 
 
4
#define PY_SSIZE_T_CLEAN
 
5
 
 
6
#include "Python.h"
 
7
 
 
8
typedef short PyInt16;
 
9
 
 
10
#if defined(__CHAR_UNSIGNED__)
 
11
#if defined(signed)
 
12
/* This module currently does not work on systems where only unsigned
 
13
   characters are available.  Take it out of Setup.  Sorry. */
 
14
#endif
 
15
#endif
 
16
 
 
17
static const int maxvals[] = {0, 0x7F, 0x7FFF, 0x7FFFFF, 0x7FFFFFFF};
 
18
/* -1 trick is needed on Windows to support -0x80000000 without a warning */
 
19
static const int minvals[] = {0, -0x80, -0x8000, -0x800000, -0x7FFFFFFF-1};
 
20
static const unsigned int masks[] = {0, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF};
 
21
 
 
22
static int
 
23
fbound(double val, double minval, double maxval)
 
24
{
 
25
    if (val > maxval)
 
26
        val = maxval;
 
27
    else if (val < minval + 1)
 
28
        val = minval;
 
29
    return (int)val;
 
30
}
 
31
 
 
32
 
 
33
/* Code shamelessly stolen from sox, 12.17.7, g711.c
 
34
** (c) Craig Reese, Joe Campbell and Jeff Poskanzer 1989 */
 
35
 
 
36
/* From g711.c:
 
37
 *
 
38
 * December 30, 1994:
 
39
 * Functions linear2alaw, linear2ulaw have been updated to correctly
 
40
 * convert unquantized 16 bit values.
 
41
 * Tables for direct u- to A-law and A- to u-law conversions have been
 
42
 * corrected.
 
43
 * Borge Lindberg, Center for PersonKommunikation, Aalborg University.
 
44
 * bli@cpk.auc.dk
 
45
 *
 
46
 */
 
47
#define BIAS 0x84   /* define the add-in bias for 16 bit samples */
 
48
#define CLIP 32635
 
49
#define SIGN_BIT        (0x80)          /* Sign bit for a A-law byte. */
 
50
#define QUANT_MASK      (0xf)           /* Quantization field mask. */
 
51
#define SEG_SHIFT       (4)             /* Left shift for segment number. */
 
52
#define SEG_MASK        (0x70)          /* Segment field mask. */
 
53
 
 
54
static PyInt16 seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
 
55
                              0x1FF, 0x3FF, 0x7FF, 0xFFF};
 
56
static PyInt16 seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
 
57
                              0x3FF, 0x7FF, 0xFFF, 0x1FFF};
 
58
 
 
59
static PyInt16
 
60
search(PyInt16 val, PyInt16 *table, int size)
 
61
{
 
62
    int i;
 
63
 
 
64
    for (i = 0; i < size; i++) {
 
65
        if (val <= *table++)
 
66
            return (i);
 
67
    }
 
68
    return (size);
 
69
}
 
70
#define st_ulaw2linear16(uc) (_st_ulaw2linear16[uc])
 
71
#define st_alaw2linear16(uc) (_st_alaw2linear16[uc])
 
72
 
 
73
static PyInt16 _st_ulaw2linear16[256] = {
 
74
    -32124,  -31100,  -30076,  -29052,  -28028,  -27004,  -25980,
 
75
    -24956,  -23932,  -22908,  -21884,  -20860,  -19836,  -18812,
 
76
    -17788,  -16764,  -15996,  -15484,  -14972,  -14460,  -13948,
 
77
    -13436,  -12924,  -12412,  -11900,  -11388,  -10876,  -10364,
 
78
     -9852,   -9340,   -8828,   -8316,   -7932,   -7676,   -7420,
 
79
     -7164,   -6908,   -6652,   -6396,   -6140,   -5884,   -5628,
 
80
     -5372,   -5116,   -4860,   -4604,   -4348,   -4092,   -3900,
 
81
     -3772,   -3644,   -3516,   -3388,   -3260,   -3132,   -3004,
 
82
     -2876,   -2748,   -2620,   -2492,   -2364,   -2236,   -2108,
 
83
     -1980,   -1884,   -1820,   -1756,   -1692,   -1628,   -1564,
 
84
     -1500,   -1436,   -1372,   -1308,   -1244,   -1180,   -1116,
 
85
     -1052,    -988,    -924,    -876,    -844,    -812,    -780,
 
86
      -748,    -716,    -684,    -652,    -620,    -588,    -556,
 
87
      -524,    -492,    -460,    -428,    -396,    -372,    -356,
 
88
      -340,    -324,    -308,    -292,    -276,    -260,    -244,
 
89
      -228,    -212,    -196,    -180,    -164,    -148,    -132,
 
90
      -120,    -112,    -104,     -96,     -88,     -80,     -72,
 
91
       -64,     -56,     -48,     -40,     -32,     -24,     -16,
 
92
    -8,       0,   32124,   31100,   30076,   29052,   28028,
 
93
     27004,   25980,   24956,   23932,   22908,   21884,   20860,
 
94
     19836,   18812,   17788,   16764,   15996,   15484,   14972,
 
95
     14460,   13948,   13436,   12924,   12412,   11900,   11388,
 
96
     10876,   10364,    9852,    9340,    8828,    8316,    7932,
 
97
      7676,    7420,    7164,    6908,    6652,    6396,    6140,
 
98
      5884,    5628,    5372,    5116,    4860,    4604,    4348,
 
99
      4092,    3900,    3772,    3644,    3516,    3388,    3260,
 
100
      3132,    3004,    2876,    2748,    2620,    2492,    2364,
 
101
      2236,    2108,    1980,    1884,    1820,    1756,    1692,
 
102
      1628,    1564,    1500,    1436,    1372,    1308,    1244,
 
103
      1180,    1116,    1052,     988,     924,     876,     844,
 
104
       812,     780,     748,     716,     684,     652,     620,
 
105
       588,     556,     524,     492,     460,     428,     396,
 
106
       372,     356,     340,     324,     308,     292,     276,
 
107
       260,     244,     228,     212,     196,     180,     164,
 
108
       148,     132,     120,     112,     104,      96,      88,
 
109
    80,      72,      64,      56,      48,      40,      32,
 
110
    24,      16,       8,       0
 
111
};
 
112
 
 
113
/*
 
114
 * linear2ulaw() accepts a 14-bit signed integer and encodes it as u-law data
 
115
 * stored in a unsigned char.  This function should only be called with
 
116
 * the data shifted such that it only contains information in the lower
 
117
 * 14-bits.
 
118
 *
 
119
 * In order to simplify the encoding process, the original linear magnitude
 
120
 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to
 
121
 * (33 - 8191). The result can be seen in the following encoding table:
 
122
 *
 
123
 *      Biased Linear Input Code        Compressed Code
 
124
 *      ------------------------        ---------------
 
125
 *      00000001wxyza                   000wxyz
 
126
 *      0000001wxyzab                   001wxyz
 
127
 *      000001wxyzabc                   010wxyz
 
128
 *      00001wxyzabcd                   011wxyz
 
129
 *      0001wxyzabcde                   100wxyz
 
130
 *      001wxyzabcdef                   101wxyz
 
131
 *      01wxyzabcdefg                   110wxyz
 
132
 *      1wxyzabcdefgh                   111wxyz
 
133
 *
 
134
 * Each biased linear code has a leading 1 which identifies the segment
 
135
 * number. The value of the segment number is equal to 7 minus the number
 
136
 * of leading 0's. The quantization interval is directly available as the
 
137
 * four bits wxyz.  * The trailing bits (a - h) are ignored.
 
138
 *
 
139
 * Ordinarily the complement of the resulting code word is used for
 
140
 * transmission, and so the code word is complemented before it is returned.
 
141
 *
 
142
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 
143
 * John Wiley & Sons, pps 98-111 and 472-476.
 
144
 */
 
145
static unsigned char
 
146
st_14linear2ulaw(PyInt16 pcm_val)       /* 2's complement (14-bit range) */
 
147
{
 
148
    PyInt16         mask;
 
149
    PyInt16         seg;
 
150
    unsigned char   uval;
 
151
 
 
152
    /* u-law inverts all bits */
 
153
    /* Get the sign and the magnitude of the value. */
 
154
    if (pcm_val < 0) {
 
155
        pcm_val = -pcm_val;
 
156
        mask = 0x7F;
 
157
    } else {
 
158
        mask = 0xFF;
 
159
    }
 
160
    if ( pcm_val > CLIP ) pcm_val = CLIP;           /* clip the magnitude */
 
161
    pcm_val += (BIAS >> 2);
 
162
 
 
163
    /* Convert the scaled magnitude to segment number. */
 
164
    seg = search(pcm_val, seg_uend, 8);
 
165
 
 
166
    /*
 
167
     * Combine the sign, segment, quantization bits;
 
168
     * and complement the code word.
 
169
     */
 
170
    if (seg >= 8)           /* out of range, return maximum value. */
 
171
        return (unsigned char) (0x7F ^ mask);
 
172
    else {
 
173
        uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
 
174
        return (uval ^ mask);
 
175
    }
 
176
 
 
177
}
 
178
 
 
179
static PyInt16 _st_alaw2linear16[256] = {
 
180
     -5504,   -5248,   -6016,   -5760,   -4480,   -4224,   -4992,
 
181
     -4736,   -7552,   -7296,   -8064,   -7808,   -6528,   -6272,
 
182
     -7040,   -6784,   -2752,   -2624,   -3008,   -2880,   -2240,
 
183
     -2112,   -2496,   -2368,   -3776,   -3648,   -4032,   -3904,
 
184
     -3264,   -3136,   -3520,   -3392,  -22016,  -20992,  -24064,
 
185
    -23040,  -17920,  -16896,  -19968,  -18944,  -30208,  -29184,
 
186
    -32256,  -31232,  -26112,  -25088,  -28160,  -27136,  -11008,
 
187
    -10496,  -12032,  -11520,   -8960,   -8448,   -9984,   -9472,
 
188
    -15104,  -14592,  -16128,  -15616,  -13056,  -12544,  -14080,
 
189
    -13568,    -344,    -328,    -376,    -360,    -280,    -264,
 
190
      -312,    -296,    -472,    -456,    -504,    -488,    -408,
 
191
      -392,    -440,    -424,     -88,     -72,    -120,    -104,
 
192
       -24,      -8,     -56,     -40,    -216,    -200,    -248,
 
193
      -232,    -152,    -136,    -184,    -168,   -1376,   -1312,
 
194
     -1504,   -1440,   -1120,   -1056,   -1248,   -1184,   -1888,
 
195
     -1824,   -2016,   -1952,   -1632,   -1568,   -1760,   -1696,
 
196
      -688,    -656,    -752,    -720,    -560,    -528,    -624,
 
197
      -592,    -944,    -912,   -1008,    -976,    -816,    -784,
 
198
      -880,    -848,    5504,    5248,    6016,    5760,    4480,
 
199
      4224,    4992,    4736,    7552,    7296,    8064,    7808,
 
200
      6528,    6272,    7040,    6784,    2752,    2624,    3008,
 
201
      2880,    2240,    2112,    2496,    2368,    3776,    3648,
 
202
      4032,    3904,    3264,    3136,    3520,    3392,   22016,
 
203
     20992,   24064,   23040,   17920,   16896,   19968,   18944,
 
204
     30208,   29184,   32256,   31232,   26112,   25088,   28160,
 
205
     27136,   11008,   10496,   12032,   11520,    8960,    8448,
 
206
      9984,    9472,   15104,   14592,   16128,   15616,   13056,
 
207
     12544,   14080,   13568,     344,     328,     376,     360,
 
208
       280,     264,     312,     296,     472,     456,     504,
 
209
       488,     408,     392,     440,     424,      88,      72,
 
210
       120,     104,      24,       8,      56,      40,     216,
 
211
       200,     248,     232,     152,     136,     184,     168,
 
212
      1376,    1312,    1504,    1440,    1120,    1056,    1248,
 
213
      1184,    1888,    1824,    2016,    1952,    1632,    1568,
 
214
      1760,    1696,     688,     656,     752,     720,     560,
 
215
       528,     624,     592,     944,     912,    1008,     976,
 
216
       816,     784,     880,     848
 
217
};
 
218
 
 
219
/*
 
220
 * linear2alaw() accepts an 13-bit signed integer and encodes it as A-law data
 
221
 * stored in a unsigned char.  This function should only be called with
 
222
 * the data shifted such that it only contains information in the lower
 
223
 * 13-bits.
 
224
 *
 
225
 *              Linear Input Code       Compressed Code
 
226
 *      ------------------------        ---------------
 
227
 *      0000000wxyza                    000wxyz
 
228
 *      0000001wxyza                    001wxyz
 
229
 *      000001wxyzab                    010wxyz
 
230
 *      00001wxyzabc                    011wxyz
 
231
 *      0001wxyzabcd                    100wxyz
 
232
 *      001wxyzabcde                    101wxyz
 
233
 *      01wxyzabcdef                    110wxyz
 
234
 *      1wxyzabcdefg                    111wxyz
 
235
 *
 
236
 * For further information see John C. Bellamy's Digital Telephony, 1982,
 
237
 * John Wiley & Sons, pps 98-111 and 472-476.
 
238
 */
 
239
static unsigned char
 
240
st_linear2alaw(PyInt16 pcm_val) /* 2's complement (13-bit range) */
 
241
{
 
242
    PyInt16         mask;
 
243
    short           seg;
 
244
    unsigned char   aval;
 
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
    }
 
269
}
 
270
/* End of code taken from sox */
 
271
 
 
272
/* Intel ADPCM step variation table */
 
273
static int indexTable[16] = {
 
274
    -1, -1, -1, -1, 2, 4, 6, 8,
 
275
    -1, -1, -1, -1, 2, 4, 6, 8,
 
276
};
 
277
 
 
278
static 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
 
288
};
 
289
 
 
290
#define GETINTX(T, cp, i)  (*(T *)((unsigned char *)(cp) + (i)))
 
291
#define SETINTX(T, cp, i, val)  do {                    \
 
292
        *(T *)((unsigned char *)(cp) + (i)) = (T)(val); \
 
293
    } while (0)
 
294
 
 
295
 
 
296
#define GETINT8(cp, i)          GETINTX(signed char, (cp), (i))
 
297
#define GETINT16(cp, i)         GETINTX(short, (cp), (i))
 
298
#define GETINT32(cp, i)         GETINTX(PY_INT32_T, (cp), (i))
 
299
 
 
300
#if WORDS_BIGENDIAN
 
301
#define GETINT24(cp, i)  (                              \
 
302
        ((unsigned char *)(cp) + (i))[2] +              \
 
303
        (((unsigned char *)(cp) + (i))[1] << 8) +       \
 
304
        (((signed char *)(cp) + (i))[0] << 16) )
 
305
#else
 
306
#define GETINT24(cp, i)  (                              \
 
307
        ((unsigned char *)(cp) + (i))[0] +              \
 
308
        (((unsigned char *)(cp) + (i))[1] << 8) +       \
 
309
        (((signed char *)(cp) + (i))[2] << 16) )
 
310
#endif
 
311
 
 
312
 
 
313
#define SETINT8(cp, i, val)     SETINTX(signed char, (cp), (i), (val))
 
314
#define SETINT16(cp, i, val)    SETINTX(short, (cp), (i), (val))
 
315
#define SETINT32(cp, i, val)    SETINTX(PY_INT32_T, (cp), (i), (val))
 
316
 
 
317
#if WORDS_BIGENDIAN
 
318
#define SETINT24(cp, i, val)  do {                              \
 
319
        ((unsigned char *)(cp) + (i))[2] = (int)(val);          \
 
320
        ((unsigned char *)(cp) + (i))[1] = (int)(val) >> 8;     \
 
321
        ((signed char *)(cp) + (i))[0] = (int)(val) >> 16;      \
 
322
    } while (0)
 
323
#else
 
324
#define SETINT24(cp, i, val)  do {                              \
 
325
        ((unsigned char *)(cp) + (i))[0] = (int)(val);          \
 
326
        ((unsigned char *)(cp) + (i))[1] = (int)(val) >> 8;     \
 
327
        ((signed char *)(cp) + (i))[2] = (int)(val) >> 16;      \
 
328
    } while (0)
 
329
#endif
 
330
 
 
331
 
 
332
#define GETRAWSAMPLE(size, cp, i)  (                    \
 
333
        (size == 1) ? (int)GETINT8((cp), (i)) :         \
 
334
        (size == 2) ? (int)GETINT16((cp), (i)) :        \
 
335
        (size == 3) ? (int)GETINT24((cp), (i)) :        \
 
336
                      (int)GETINT32((cp), (i)))
 
337
 
 
338
#define SETRAWSAMPLE(size, cp, i, val)  do {    \
 
339
        if (size == 1)                          \
 
340
            SETINT8((cp), (i), (val));          \
 
341
        else if (size == 2)                     \
 
342
            SETINT16((cp), (i), (val));         \
 
343
        else if (size == 3)                     \
 
344
            SETINT24((cp), (i), (val));         \
 
345
        else                                    \
 
346
            SETINT32((cp), (i), (val));         \
 
347
    } while(0)
 
348
 
 
349
 
 
350
#define GETSAMPLE32(size, cp, i)  (                     \
 
351
        (size == 1) ? (int)GETINT8((cp), (i)) << 24 :   \
 
352
        (size == 2) ? (int)GETINT16((cp), (i)) << 16 :  \
 
353
        (size == 3) ? (int)GETINT24((cp), (i)) << 8 :   \
 
354
                      (int)GETINT32((cp), (i)))
 
355
 
 
356
#define SETSAMPLE32(size, cp, i, val)  do {     \
 
357
        if (size == 1)                          \
 
358
            SETINT8((cp), (i), (val) >> 24);    \
 
359
        else if (size == 2)                     \
 
360
            SETINT16((cp), (i), (val) >> 16);   \
 
361
        else if (size == 3)                     \
 
362
            SETINT24((cp), (i), (val) >> 8);    \
 
363
        else                                    \
 
364
            SETINT32((cp), (i), (val));         \
 
365
    } while(0)
 
366
 
 
367
 
 
368
static PyObject *AudioopError;
 
369
 
 
370
static int
 
371
audioop_check_size(int size)
 
372
{
 
373
    if (size < 1 || size > 4) {
 
374
        PyErr_SetString(AudioopError, "Size should be 1, 2, 3 or 4");
 
375
        return 0;
 
376
    }
 
377
    else
 
378
        return 1;
 
379
}
 
380
 
 
381
static int
 
382
audioop_check_parameters(Py_ssize_t len, int size)
 
383
{
 
384
    if (!audioop_check_size(size))
 
385
        return 0;
 
386
    if (len % size != 0) {
 
387
        PyErr_SetString(AudioopError, "not a whole number of frames");
 
388
        return 0;
 
389
    }
 
390
    return 1;
 
391
}
 
392
 
 
393
static PyObject *
 
394
audioop_getsample(PyObject *self, PyObject *args)
 
395
{
 
396
    Py_buffer view;
 
397
    Py_ssize_t i;
 
398
    int size;
 
399
    int val;
 
400
 
 
401
    if (!PyArg_ParseTuple(args, "y*in:getsample", &view, &size, &i))
 
402
        return NULL;
 
403
    if (!audioop_check_parameters(view.len, size))
 
404
        goto error;
 
405
    if (i < 0 || i >= view.len/size) {
 
406
        PyErr_SetString(AudioopError, "Index out of range");
 
407
        goto error;
 
408
    }
 
409
    val = GETRAWSAMPLE(size, view.buf, i*size);
 
410
    PyBuffer_Release(&view);
 
411
    return PyLong_FromLong(val);
 
412
 
 
413
  error:
 
414
    PyBuffer_Release(&view);
 
415
    return NULL;
 
416
}
 
417
 
 
418
static PyObject *
 
419
audioop_max(PyObject *self, PyObject *args)
 
420
{
 
421
    Py_buffer view;
 
422
    Py_ssize_t i;
 
423
    int size;
 
424
    unsigned int absval, max = 0;
 
425
 
 
426
    if (!PyArg_ParseTuple(args, "y*i:max", &view, &size))
 
427
        return NULL;
 
428
    if (!audioop_check_parameters(view.len, size)) {
 
429
        PyBuffer_Release(&view);
 
430
        return NULL;
 
431
    }
 
432
    for (i = 0; i < view.len; i += size) {
 
433
        int val = GETRAWSAMPLE(size, view.buf, i);
 
434
        if (val < 0) absval = (-val);
 
435
        else absval = val;
 
436
        if (absval > max) max = absval;
 
437
    }
 
438
    PyBuffer_Release(&view);
 
439
    return PyLong_FromUnsignedLong(max);
 
440
}
 
441
 
 
442
static PyObject *
 
443
audioop_minmax(PyObject *self, PyObject *args)
 
444
{
 
445
    Py_buffer view;
 
446
    Py_ssize_t i;
 
447
    int size;
 
448
    /* -1 trick below is needed on Windows to support -0x80000000 without
 
449
    a warning */
 
450
    int min = 0x7fffffff, max = -0x7FFFFFFF-1;
 
451
 
 
452
    if (!PyArg_ParseTuple(args, "y*i:minmax", &view, &size))
 
453
        return NULL;
 
454
    if (!audioop_check_parameters(view.len, size)) {
 
455
        PyBuffer_Release(&view);
 
456
        return NULL;
 
457
    }
 
458
    for (i = 0; i < view.len; i += size) {
 
459
        int val = GETRAWSAMPLE(size, view.buf, i);
 
460
        if (val > max) max = val;
 
461
        if (val < min) min = val;
 
462
    }
 
463
    PyBuffer_Release(&view);
 
464
    return Py_BuildValue("(ii)", min, max);
 
465
}
 
466
 
 
467
static PyObject *
 
468
audioop_avg(PyObject *self, PyObject *args)
 
469
{
 
470
    Py_buffer view;
 
471
    Py_ssize_t i;
 
472
    int size, avg;
 
473
    double sum = 0.0;
 
474
 
 
475
    if (!PyArg_ParseTuple(args, "y*i:avg", &view, &size))
 
476
        return NULL;
 
477
    if (!audioop_check_parameters(view.len, size)) {
 
478
        PyBuffer_Release(&view);
 
479
        return NULL;
 
480
    }
 
481
    for (i = 0; i < view.len; i += size)
 
482
        sum += GETRAWSAMPLE(size, view.buf, i);
 
483
    if (view.len == 0)
 
484
        avg = 0;
 
485
    else
 
486
        avg = (int)floor(sum / (double)(view.len/size));
 
487
    PyBuffer_Release(&view);
 
488
    return PyLong_FromLong(avg);
 
489
}
 
490
 
 
491
static PyObject *
 
492
audioop_rms(PyObject *self, PyObject *args)
 
493
{
 
494
    Py_buffer view;
 
495
    Py_ssize_t i;
 
496
    int size;
 
497
    unsigned int res;
 
498
    double sum_squares = 0.0;
 
499
 
 
500
    if (!PyArg_ParseTuple(args, "y*i:rms", &view, &size))
 
501
        return NULL;
 
502
    if (!audioop_check_parameters(view.len, size)) {
 
503
        PyBuffer_Release(&view);
 
504
        return NULL;
 
505
    }
 
506
    for (i = 0; i < view.len; i += size) {
 
507
        double val = GETRAWSAMPLE(size, view.buf, i);
 
508
        sum_squares += val*val;
 
509
    }
 
510
    if (view.len == 0)
 
511
        res = 0;
 
512
    else
 
513
        res = (unsigned int)sqrt(sum_squares / (double)(view.len/size));
 
514
    PyBuffer_Release(&view);
 
515
    return PyLong_FromUnsignedLong(res);
 
516
}
 
517
 
 
518
static double _sum2(const short *a, const short *b, Py_ssize_t len)
 
519
{
 
520
    Py_ssize_t i;
 
521
    double sum = 0.0;
 
522
 
 
523
    for( i=0; i<len; i++) {
 
524
        sum = sum + (double)a[i]*(double)b[i];
 
525
    }
 
526
    return sum;
 
527
}
 
528
 
 
529
/*
 
530
** Findfit tries to locate a sample within another sample. Its main use
 
531
** is in echo-cancellation (to find the feedback of the output signal in
 
532
** the input signal).
 
533
** The method used is as follows:
 
534
**
 
535
** let R be the reference signal (length n) and A the input signal (length N)
 
536
** with N > n, and let all sums be over i from 0 to n-1.
 
537
**
 
538
** Now, for each j in {0..N-n} we compute a factor fj so that -fj*R matches A
 
539
** as good as possible, i.e. sum( (A[j+i]+fj*R[i])^2 ) is minimal. This
 
540
** equation gives fj = sum( A[j+i]R[i] ) / sum(R[i]^2).
 
541
**
 
542
** Next, we compute the relative distance between the original signal and
 
543
** the modified signal and minimize that over j:
 
544
** vj = sum( (A[j+i]-fj*R[i])^2 ) / sum( A[j+i]^2 )  =>
 
545
** vj = ( sum(A[j+i]^2)*sum(R[i]^2) - sum(A[j+i]R[i])^2 ) / sum( A[j+i]^2 )
 
546
**
 
547
** In the code variables correspond as follows:
 
548
** cp1          A
 
549
** cp2          R
 
550
** len1         N
 
551
** len2         n
 
552
** aj_m1        A[j-1]
 
553
** aj_lm1       A[j+n-1]
 
554
** sum_ri_2     sum(R[i]^2)
 
555
** sum_aij_2    sum(A[i+j]^2)
 
556
** sum_aij_ri   sum(A[i+j]R[i])
 
557
**
 
558
** sum_ri is calculated once, sum_aij_2 is updated each step and sum_aij_ri
 
559
** is completely recalculated each step.
 
560
*/
 
561
static PyObject *
 
562
audioop_findfit(PyObject *self, PyObject *args)
 
563
{
 
564
    Py_buffer view1;
 
565
    Py_buffer view2;
 
566
    const short *cp1, *cp2;
 
567
    Py_ssize_t len1, len2;
 
568
    Py_ssize_t j, best_j;
 
569
    double aj_m1, aj_lm1;
 
570
    double sum_ri_2, sum_aij_2, sum_aij_ri, result, best_result, factor;
 
571
 
 
572
    if (!PyArg_ParseTuple(args, "y*y*:findfit", &view1, &view2))
 
573
        return NULL;
 
574
    if (view1.len & 1 || view2.len & 1) {
 
575
        PyErr_SetString(AudioopError, "Strings should be even-sized");
 
576
        goto error;
 
577
    }
 
578
    cp1 = (const short *)view1.buf;
 
579
    len1 = view1.len >> 1;
 
580
    cp2 = (const short *)view2.buf;
 
581
    len2 = view2.len >> 1;
 
582
 
 
583
    if (len1 < len2) {
 
584
        PyErr_SetString(AudioopError, "First sample should be longer");
 
585
        goto error;
 
586
    }
 
587
    sum_ri_2 = _sum2(cp2, cp2, len2);
 
588
    sum_aij_2 = _sum2(cp1, cp1, len2);
 
589
    sum_aij_ri = _sum2(cp1, cp2, len2);
 
590
 
 
591
    result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri) / sum_aij_2;
 
592
 
 
593
    best_result = result;
 
594
    best_j = 0;
 
595
 
 
596
    for ( j=1; j<=len1-len2; j++) {
 
597
        aj_m1 = (double)cp1[j-1];
 
598
        aj_lm1 = (double)cp1[j+len2-1];
 
599
 
 
600
        sum_aij_2 = sum_aij_2 + aj_lm1*aj_lm1 - aj_m1*aj_m1;
 
601
        sum_aij_ri = _sum2(cp1+j, cp2, len2);
 
602
 
 
603
        result = (sum_ri_2*sum_aij_2 - sum_aij_ri*sum_aij_ri)
 
604
            / sum_aij_2;
 
605
 
 
606
        if ( result < best_result ) {
 
607
            best_result = result;
 
608
            best_j = j;
 
609
        }
 
610
 
 
611
    }
 
612
 
 
613
    factor = _sum2(cp1+best_j, cp2, len2) / sum_ri_2;
 
614
 
 
615
    PyBuffer_Release(&view1);
 
616
    PyBuffer_Release(&view2);
 
617
    return Py_BuildValue("(nf)", best_j, factor);
 
618
 
 
619
  error:
 
620
    PyBuffer_Release(&view1);
 
621
    PyBuffer_Release(&view2);
 
622
    return NULL;
 
623
}
 
624
 
 
625
/*
 
626
** findfactor finds a factor f so that the energy in A-fB is minimal.
 
627
** See the comment for findfit for details.
 
628
*/
 
629
static PyObject *
 
630
audioop_findfactor(PyObject *self, PyObject *args)
 
631
{
 
632
    Py_buffer view1;
 
633
    Py_buffer view2;
 
634
    const short *cp1, *cp2;
 
635
    Py_ssize_t len;
 
636
    double sum_ri_2, sum_aij_ri, result;
 
637
 
 
638
    if (!PyArg_ParseTuple(args, "y*y*:findfactor", &view1, &view2))
 
639
        return NULL;
 
640
    if (view1.len & 1 || view2.len & 1) {
 
641
        PyErr_SetString(AudioopError, "Strings should be even-sized");
 
642
        goto error;
 
643
    }
 
644
    if (view1.len != view2.len) {
 
645
        PyErr_SetString(AudioopError, "Samples should be same size");
 
646
        goto error;
 
647
    }
 
648
    cp1 = (const short *)view1.buf;
 
649
    cp2 = (const short *)view2.buf;
 
650
    len = view1.len >> 1;
 
651
    sum_ri_2 = _sum2(cp2, cp2, len);
 
652
    sum_aij_ri = _sum2(cp1, cp2, len);
 
653
 
 
654
    result = sum_aij_ri / sum_ri_2;
 
655
 
 
656
    PyBuffer_Release(&view1);
 
657
    PyBuffer_Release(&view2);
 
658
    return PyFloat_FromDouble(result);
 
659
 
 
660
  error:
 
661
    PyBuffer_Release(&view1);
 
662
    PyBuffer_Release(&view2);
 
663
    return NULL;
 
664
}
 
665
 
 
666
/*
 
667
** findmax returns the index of the n-sized segment of the input sample
 
668
** that contains the most energy.
 
669
*/
 
670
static PyObject *
 
671
audioop_findmax(PyObject *self, PyObject *args)
 
672
{
 
673
    Py_buffer view;
 
674
    const short *cp1;
 
675
    Py_ssize_t len1, len2;
 
676
    Py_ssize_t j, best_j;
 
677
    double aj_m1, aj_lm1;
 
678
    double result, best_result;
 
679
 
 
680
    if (!PyArg_ParseTuple(args, "y*n:findmax", &view, &len2))
 
681
        return NULL;
 
682
    if (view.len & 1) {
 
683
        PyErr_SetString(AudioopError, "Strings should be even-sized");
 
684
        goto error;
 
685
    }
 
686
    cp1 = (const short *)view.buf;
 
687
    len1 = view.len >> 1;
 
688
 
 
689
    if (len2 < 0 || len1 < len2) {
 
690
        PyErr_SetString(AudioopError, "Input sample should be longer");
 
691
        goto error;
 
692
    }
 
693
 
 
694
    result = _sum2(cp1, cp1, len2);
 
695
 
 
696
    best_result = result;
 
697
    best_j = 0;
 
698
 
 
699
    for ( j=1; j<=len1-len2; j++) {
 
700
        aj_m1 = (double)cp1[j-1];
 
701
        aj_lm1 = (double)cp1[j+len2-1];
 
702
 
 
703
        result = result + aj_lm1*aj_lm1 - aj_m1*aj_m1;
 
704
 
 
705
        if ( result > best_result ) {
 
706
            best_result = result;
 
707
            best_j = j;
 
708
        }
 
709
 
 
710
    }
 
711
 
 
712
    PyBuffer_Release(&view);
 
713
    return PyLong_FromSsize_t(best_j);
 
714
 
 
715
  error:
 
716
    PyBuffer_Release(&view);
 
717
    return NULL;
 
718
}
 
719
 
 
720
static PyObject *
 
721
audioop_avgpp(PyObject *self, PyObject *args)
 
722
{
 
723
    Py_buffer view;
 
724
    Py_ssize_t i;
 
725
    int size, prevval, prevextremevalid = 0,
 
726
        prevextreme = 0;
 
727
    double sum = 0.0;
 
728
    unsigned int avg;
 
729
    int diff, prevdiff, nextreme = 0;
 
730
 
 
731
    if (!PyArg_ParseTuple(args, "y*i:avgpp", &view, &size))
 
732
        return NULL;
 
733
    if (!audioop_check_parameters(view.len, size)) {
 
734
        PyBuffer_Release(&view);
 
735
        return NULL;
 
736
    }
 
737
    if (view.len <= size) {
 
738
        PyBuffer_Release(&view);
 
739
        return PyLong_FromLong(0);
 
740
    }
 
741
    prevval = GETRAWSAMPLE(size, view.buf, 0);
 
742
    prevdiff = 17; /* Anything != 0, 1 */
 
743
    for (i = size; i < view.len; i += size) {
 
744
        int val = GETRAWSAMPLE(size, view.buf, i);
 
745
        if (val != prevval) {
 
746
            diff = val < prevval;
 
747
            if (prevdiff == !diff) {
 
748
                /* Derivative changed sign. Compute difference to last
 
749
                ** extreme value and remember.
 
750
                */
 
751
                if (prevextremevalid) {
 
752
                    if (prevval < prevextreme)
 
753
                        sum += (double)((unsigned int)prevextreme -
 
754
                                        (unsigned int)prevval);
 
755
                    else
 
756
                        sum += (double)((unsigned int)prevval -
 
757
                                        (unsigned int)prevextreme);
 
758
                    nextreme++;
 
759
                }
 
760
                prevextremevalid = 1;
 
761
                prevextreme = prevval;
 
762
            }
 
763
            prevval = val;
 
764
            prevdiff = diff;
 
765
        }
 
766
    }
 
767
    if ( nextreme == 0 )
 
768
        avg = 0;
 
769
    else
 
770
        avg = (unsigned int)(sum / (double)nextreme);
 
771
    PyBuffer_Release(&view);
 
772
    return PyLong_FromUnsignedLong(avg);
 
773
}
 
774
 
 
775
static PyObject *
 
776
audioop_maxpp(PyObject *self, PyObject *args)
 
777
{
 
778
    Py_buffer view;
 
779
    Py_ssize_t i;
 
780
    int size, prevval, prevextremevalid = 0,
 
781
        prevextreme = 0;
 
782
    unsigned int max = 0, extremediff;
 
783
    int diff, prevdiff;
 
784
 
 
785
    if (!PyArg_ParseTuple(args, "y*i:maxpp", &view, &size))
 
786
        return NULL;
 
787
    if (!audioop_check_parameters(view.len, size)) {
 
788
        PyBuffer_Release(&view);
 
789
        return NULL;
 
790
    }
 
791
    if (view.len <= size) {
 
792
        PyBuffer_Release(&view);
 
793
        return PyLong_FromLong(0);
 
794
    }
 
795
    prevval = GETRAWSAMPLE(size, view.buf, 0);
 
796
    prevdiff = 17; /* Anything != 0, 1 */
 
797
    for (i = size; i < view.len; i += size) {
 
798
        int val = GETRAWSAMPLE(size, view.buf, i);
 
799
        if (val != prevval) {
 
800
            diff = val < prevval;
 
801
            if (prevdiff == !diff) {
 
802
                /* Derivative changed sign. Compute difference to
 
803
                ** last extreme value and remember.
 
804
                */
 
805
                if (prevextremevalid) {
 
806
                    if (prevval < prevextreme)
 
807
                        extremediff = (unsigned int)prevextreme -
 
808
                                      (unsigned int)prevval;
 
809
                    else
 
810
                        extremediff = (unsigned int)prevval -
 
811
                                      (unsigned int)prevextreme;
 
812
                    if ( extremediff > max )
 
813
                        max = extremediff;
 
814
                }
 
815
                prevextremevalid = 1;
 
816
                prevextreme = prevval;
 
817
            }
 
818
            prevval = val;
 
819
            prevdiff = diff;
 
820
        }
 
821
    }
 
822
    PyBuffer_Release(&view);
 
823
    return PyLong_FromUnsignedLong(max);
 
824
}
 
825
 
 
826
static PyObject *
 
827
audioop_cross(PyObject *self, PyObject *args)
 
828
{
 
829
    Py_buffer view;
 
830
    Py_ssize_t i;
 
831
    int size;
 
832
    int prevval;
 
833
    Py_ssize_t ncross;
 
834
 
 
835
    if (!PyArg_ParseTuple(args, "y*i:cross", &view, &size))
 
836
        return NULL;
 
837
    if (!audioop_check_parameters(view.len, size)) {
 
838
        PyBuffer_Release(&view);
 
839
        return NULL;
 
840
    }
 
841
    ncross = -1;
 
842
    prevval = 17; /* Anything <> 0,1 */
 
843
    for (i = 0; i < view.len; i += size) {
 
844
        int val = GETRAWSAMPLE(size, view.buf, i) < 0;
 
845
        if (val != prevval) ncross++;
 
846
        prevval = val;
 
847
    }
 
848
    PyBuffer_Release(&view);
 
849
    return PyLong_FromSsize_t(ncross);
 
850
}
 
851
 
 
852
static PyObject *
 
853
audioop_mul(PyObject *self, PyObject *args)
 
854
{
 
855
    Py_buffer view;
 
856
    signed char *ncp;
 
857
    Py_ssize_t i;
 
858
    int size;
 
859
    double factor, maxval, minval;
 
860
    PyObject *rv = NULL;
 
861
 
 
862
    if (!PyArg_ParseTuple(args, "y*id:mul", &view, &size, &factor))
 
863
        return NULL;
 
864
    if (!audioop_check_parameters(view.len, size))
 
865
        goto exit;
 
866
 
 
867
    maxval = (double) maxvals[size];
 
868
    minval = (double) minvals[size];
 
869
 
 
870
    rv = PyBytes_FromStringAndSize(NULL, view.len);
 
871
    if (rv == NULL)
 
872
        goto exit;
 
873
    ncp = (signed char *)PyBytes_AsString(rv);
 
874
 
 
875
    for (i = 0; i < view.len; i += size) {
 
876
        double val = GETRAWSAMPLE(size, view.buf, i);
 
877
        val *= factor;
 
878
        val = floor(fbound(val, minval, maxval));
 
879
        SETRAWSAMPLE(size, ncp, i, (int)val);
 
880
    }
 
881
  exit:
 
882
    PyBuffer_Release(&view);
 
883
    return rv;
 
884
}
 
885
 
 
886
static PyObject *
 
887
audioop_tomono(PyObject *self, PyObject *args)
 
888
{
 
889
    Py_buffer pcp;
 
890
    signed char *cp, *ncp;
 
891
    Py_ssize_t len, i;
 
892
    int size;
 
893
    double fac1, fac2, maxval, minval;
 
894
    PyObject *rv = NULL;
 
895
 
 
896
    if (!PyArg_ParseTuple(args, "y*idd:tomono",
 
897
                          &pcp, &size, &fac1, &fac2))
 
898
        return NULL;
 
899
    cp = pcp.buf;
 
900
    len = pcp.len;
 
901
    if (!audioop_check_parameters(len, size))
 
902
        goto exit;
 
903
    if (((len / size) & 1) != 0) {
 
904
        PyErr_SetString(AudioopError, "not a whole number of frames");
 
905
        goto exit;
 
906
    }
 
907
 
 
908
    maxval = (double) maxvals[size];
 
909
    minval = (double) minvals[size];
 
910
 
 
911
    rv = PyBytes_FromStringAndSize(NULL, len/2);
 
912
    if (rv == NULL)
 
913
        goto exit;
 
914
    ncp = (signed char *)PyBytes_AsString(rv);
 
915
 
 
916
    for (i = 0; i < len; i += size*2) {
 
917
        double val1 = GETRAWSAMPLE(size, cp, i);
 
918
        double val2 = GETRAWSAMPLE(size, cp, i + size);
 
919
        double val = val1*fac1 + val2*fac2;
 
920
        val = floor(fbound(val, minval, maxval));
 
921
        SETRAWSAMPLE(size, ncp, i/2, val);
 
922
    }
 
923
  exit:
 
924
    PyBuffer_Release(&pcp);
 
925
    return rv;
 
926
}
 
927
 
 
928
static PyObject *
 
929
audioop_tostereo(PyObject *self, PyObject *args)
 
930
{
 
931
    Py_buffer view;
 
932
    signed char *ncp;
 
933
    Py_ssize_t i;
 
934
    int size;
 
935
    double fac1, fac2, maxval, minval;
 
936
    PyObject *rv = NULL;
 
937
 
 
938
    if (!PyArg_ParseTuple(args, "y*idd:tostereo",
 
939
                          &view, &size, &fac1, &fac2))
 
940
        return NULL;
 
941
    if (!audioop_check_parameters(view.len, size))
 
942
        goto exit;
 
943
 
 
944
    maxval = (double) maxvals[size];
 
945
    minval = (double) minvals[size];
 
946
 
 
947
    if (view.len > PY_SSIZE_T_MAX/2) {
 
948
        PyErr_SetString(PyExc_MemoryError,
 
949
                        "not enough memory for output buffer");
 
950
        goto exit;
 
951
    }
 
952
 
 
953
    rv = PyBytes_FromStringAndSize(NULL, view.len*2);
 
954
    if (rv == NULL)
 
955
        goto exit;
 
956
    ncp = (signed char *)PyBytes_AsString(rv);
 
957
 
 
958
    for (i = 0; i < view.len; i += size) {
 
959
        double val = GETRAWSAMPLE(size, view.buf, i);
 
960
        int val1 = (int)floor(fbound(val*fac1, minval, maxval));
 
961
        int val2 = (int)floor(fbound(val*fac2, minval, maxval));
 
962
        SETRAWSAMPLE(size, ncp, i*2, val1);
 
963
        SETRAWSAMPLE(size, ncp, i*2 + size, val2);
 
964
    }
 
965
  exit:
 
966
    PyBuffer_Release(&view);
 
967
    return rv;
 
968
}
 
969
 
 
970
static PyObject *
 
971
audioop_add(PyObject *self, PyObject *args)
 
972
{
 
973
    Py_buffer view1;
 
974
    Py_buffer view2;
 
975
    signed char *ncp;
 
976
    Py_ssize_t i;
 
977
    int size, minval, maxval, newval;
 
978
    PyObject *rv = NULL;
 
979
 
 
980
    if (!PyArg_ParseTuple(args, "y*y*i:add",
 
981
                          &view1, &view2, &size))
 
982
        return NULL;
 
983
    if (!audioop_check_parameters(view1.len, size))
 
984
        goto exit;
 
985
    if (view1.len != view2.len) {
 
986
        PyErr_SetString(AudioopError, "Lengths should be the same");
 
987
        goto exit;
 
988
    }
 
989
 
 
990
    maxval = maxvals[size];
 
991
    minval = minvals[size];
 
992
 
 
993
    rv = PyBytes_FromStringAndSize(NULL, view1.len);
 
994
    if (rv == NULL)
 
995
        goto exit;
 
996
    ncp = (signed char *)PyBytes_AsString(rv);
 
997
 
 
998
    for (i = 0; i < view1.len; i += size) {
 
999
        int val1 = GETRAWSAMPLE(size, view1.buf, i);
 
1000
        int val2 = GETRAWSAMPLE(size, view2.buf, i);
 
1001
 
 
1002
        if (size < 4) {
 
1003
            newval = val1 + val2;
 
1004
            /* truncate in case of overflow */
 
1005
            if (newval > maxval)
 
1006
                newval = maxval;
 
1007
            else if (newval < minval)
 
1008
                newval = minval;
 
1009
        }
 
1010
        else {
 
1011
            double fval = (double)val1 + (double)val2;
 
1012
            /* truncate in case of overflow */
 
1013
            newval = (int)floor(fbound(fval, minval, maxval));
 
1014
        }
 
1015
 
 
1016
        SETRAWSAMPLE(size, ncp, i, newval);
 
1017
    }
 
1018
  exit:
 
1019
    PyBuffer_Release(&view1);
 
1020
    PyBuffer_Release(&view2);
 
1021
    return rv;
 
1022
}
 
1023
 
 
1024
static PyObject *
 
1025
audioop_bias(PyObject *self, PyObject *args)
 
1026
{
 
1027
    Py_buffer view;
 
1028
    signed char *ncp;
 
1029
    Py_ssize_t i;
 
1030
    int size, bias;
 
1031
    unsigned int val = 0, mask;
 
1032
    PyObject *rv = NULL;
 
1033
 
 
1034
    if (!PyArg_ParseTuple(args, "y*ii:bias",
 
1035
                          &view, &size, &bias))
 
1036
        return NULL;
 
1037
 
 
1038
    if (!audioop_check_parameters(view.len, size))
 
1039
        goto exit;
 
1040
 
 
1041
    rv = PyBytes_FromStringAndSize(NULL, view.len);
 
1042
    if (rv == NULL)
 
1043
        goto exit;
 
1044
    ncp = (signed char *)PyBytes_AsString(rv);
 
1045
 
 
1046
    mask = masks[size];
 
1047
 
 
1048
    for (i = 0; i < view.len; i += size) {
 
1049
        if (size == 1)
 
1050
            val = GETINTX(unsigned char, view.buf, i);
 
1051
        else if (size == 2)
 
1052
            val = GETINTX(unsigned short, view.buf, i);
 
1053
        else if (size == 3)
 
1054
            val = ((unsigned int)GETINT24(view.buf, i)) & 0xffffffu;
 
1055
        else {
 
1056
            assert(size == 4);
 
1057
            val = GETINTX(PY_UINT32_T, view.buf, i);
 
1058
        }
 
1059
 
 
1060
        val += (unsigned int)bias;
 
1061
        /* wrap around in case of overflow */
 
1062
        val &= mask;
 
1063
 
 
1064
        if (size == 1)
 
1065
            SETINTX(unsigned char, ncp, i, val);
 
1066
        else if (size == 2)
 
1067
            SETINTX(unsigned short, ncp, i, val);
 
1068
        else if (size == 3)
 
1069
            SETINT24(ncp, i, (int)val);
 
1070
        else {
 
1071
            assert(size == 4);
 
1072
            SETINTX(PY_UINT32_T, ncp, i, val);
 
1073
        }
 
1074
    }
 
1075
  exit:
 
1076
    PyBuffer_Release(&view);
 
1077
    return rv;
 
1078
}
 
1079
 
 
1080
static PyObject *
 
1081
audioop_reverse(PyObject *self, PyObject *args)
 
1082
{
 
1083
    Py_buffer view;
 
1084
    unsigned char *ncp;
 
1085
    Py_ssize_t i;
 
1086
    int size;
 
1087
    PyObject *rv = NULL;
 
1088
 
 
1089
    if (!PyArg_ParseTuple(args, "y*i:reverse",
 
1090
                          &view, &size))
 
1091
        return NULL;
 
1092
 
 
1093
    if (!audioop_check_parameters(view.len, size))
 
1094
        goto exit;
 
1095
 
 
1096
    rv = PyBytes_FromStringAndSize(NULL, view.len);
 
1097
    if (rv == NULL)
 
1098
        goto exit;
 
1099
    ncp = (unsigned char *)PyBytes_AsString(rv);
 
1100
 
 
1101
    for (i = 0; i < view.len; i += size) {
 
1102
        int val = GETRAWSAMPLE(size, view.buf, i);
 
1103
        SETRAWSAMPLE(size, ncp, view.len - i - size, val);
 
1104
    }
 
1105
  exit:
 
1106
    PyBuffer_Release(&view);
 
1107
    return rv;
 
1108
}
 
1109
 
 
1110
static PyObject *
 
1111
audioop_byteswap(PyObject *self, PyObject *args)
 
1112
{
 
1113
    Py_buffer view;
 
1114
    unsigned char *ncp;
 
1115
    Py_ssize_t i;
 
1116
    int size;
 
1117
    PyObject *rv = NULL;
 
1118
 
 
1119
    if (!PyArg_ParseTuple(args, "y*i:swapbytes",
 
1120
                          &view, &size))
 
1121
        return NULL;
 
1122
 
 
1123
    if (!audioop_check_parameters(view.len, size))
 
1124
        goto exit;
 
1125
 
 
1126
    rv = PyBytes_FromStringAndSize(NULL, view.len);
 
1127
    if (rv == NULL)
 
1128
        goto exit;
 
1129
    ncp = (unsigned char *)PyBytes_AsString(rv);
 
1130
 
 
1131
    for (i = 0; i < view.len; i += size) {
 
1132
        int j;
 
1133
        for (j = 0; j < size; j++)
 
1134
            ncp[i + size - 1 - j] = ((unsigned char *)view.buf)[i + j];
 
1135
    }
 
1136
  exit:
 
1137
    PyBuffer_Release(&view);
 
1138
    return rv;
 
1139
}
 
1140
 
 
1141
static PyObject *
 
1142
audioop_lin2lin(PyObject *self, PyObject *args)
 
1143
{
 
1144
    Py_buffer view;
 
1145
    unsigned char *ncp;
 
1146
    Py_ssize_t i, j;
 
1147
    int size, size2;
 
1148
    PyObject *rv = NULL;
 
1149
 
 
1150
    if (!PyArg_ParseTuple(args, "y*ii:lin2lin",
 
1151
                          &view, &size, &size2))
 
1152
        return NULL;
 
1153
 
 
1154
    if (!audioop_check_parameters(view.len, size))
 
1155
        goto exit;
 
1156
    if (!audioop_check_size(size2))
 
1157
        goto exit;
 
1158
 
 
1159
    if (view.len/size > PY_SSIZE_T_MAX/size2) {
 
1160
        PyErr_SetString(PyExc_MemoryError,
 
1161
                        "not enough memory for output buffer");
 
1162
        goto exit;
 
1163
    }
 
1164
    rv = PyBytes_FromStringAndSize(NULL, (view.len/size)*size2);
 
1165
    if (rv == NULL)
 
1166
        goto exit;
 
1167
    ncp = (unsigned char *)PyBytes_AsString(rv);
 
1168
 
 
1169
    for (i = j = 0; i < view.len; i += size, j += size2) {
 
1170
        int val = GETSAMPLE32(size, view.buf, i);
 
1171
        SETSAMPLE32(size2, ncp, j, val);
 
1172
    }
 
1173
  exit:
 
1174
    PyBuffer_Release(&view);
 
1175
    return rv;
 
1176
}
 
1177
 
 
1178
static int
 
1179
gcd(int a, int b)
 
1180
{
 
1181
    while (b > 0) {
 
1182
        int tmp = a % b;
 
1183
        a = b;
 
1184
        b = tmp;
 
1185
    }
 
1186
    return a;
 
1187
}
 
1188
 
 
1189
static PyObject *
 
1190
audioop_ratecv(PyObject *self, PyObject *args)
 
1191
{
 
1192
    Py_buffer view;
 
1193
    char *cp, *ncp;
 
1194
    Py_ssize_t len;
 
1195
    int size, nchannels, inrate, outrate, weightA, weightB;
 
1196
    int chan, d, *prev_i, *cur_i, cur_o;
 
1197
    PyObject *state, *samps, *str, *rv = NULL;
 
1198
    int bytes_per_frame;
 
1199
 
 
1200
    weightA = 1;
 
1201
    weightB = 0;
 
1202
    if (!PyArg_ParseTuple(args, "y*iiiiO|ii:ratecv", &view, &size,
 
1203
                          &nchannels, &inrate, &outrate, &state,
 
1204
                          &weightA, &weightB))
 
1205
        return NULL;
 
1206
    if (!audioop_check_size(size))
 
1207
        goto exit2;
 
1208
    if (nchannels < 1) {
 
1209
        PyErr_SetString(AudioopError, "# of channels should be >= 1");
 
1210
        goto exit2;
 
1211
    }
 
1212
    if (size > INT_MAX / nchannels) {
 
1213
        /* This overflow test is rigorously correct because
 
1214
           both multiplicands are >= 1.  Use the argument names
 
1215
           from the docs for the error msg. */
 
1216
        PyErr_SetString(PyExc_OverflowError,
 
1217
                        "width * nchannels too big for a C int");
 
1218
        goto exit2;
 
1219
    }
 
1220
    bytes_per_frame = size * nchannels;
 
1221
    if (weightA < 1 || weightB < 0) {
 
1222
        PyErr_SetString(AudioopError,
 
1223
            "weightA should be >= 1, weightB should be >= 0");
 
1224
        goto exit2;
 
1225
    }
 
1226
    if (view.len % bytes_per_frame != 0) {
 
1227
        PyErr_SetString(AudioopError, "not a whole number of frames");
 
1228
        goto exit2;
 
1229
    }
 
1230
    if (inrate <= 0 || outrate <= 0) {
 
1231
        PyErr_SetString(AudioopError, "sampling rate not > 0");
 
1232
        goto exit2;
 
1233
    }
 
1234
    /* divide inrate and outrate by their greatest common divisor */
 
1235
    d = gcd(inrate, outrate);
 
1236
    inrate /= d;
 
1237
    outrate /= d;
 
1238
    /* divide weightA and weightB by their greatest common divisor */
 
1239
    d = gcd(weightA, weightB);
 
1240
    weightA /= d;
 
1241
    weightA /= d;
 
1242
 
 
1243
    if ((size_t)nchannels > PY_SIZE_MAX/sizeof(int)) {
 
1244
        PyErr_SetString(PyExc_MemoryError,
 
1245
                        "not enough memory for output buffer");
 
1246
        goto exit2;
 
1247
    }
 
1248
    prev_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
 
1249
    cur_i = (int *) PyMem_Malloc(nchannels * sizeof(int));
 
1250
    if (prev_i == NULL || cur_i == NULL) {
 
1251
        (void) PyErr_NoMemory();
 
1252
        goto exit;
 
1253
    }
 
1254
 
 
1255
    len = view.len / bytes_per_frame; /* # of frames */
 
1256
 
 
1257
    if (state == Py_None) {
 
1258
        d = -outrate;
 
1259
        for (chan = 0; chan < nchannels; chan++)
 
1260
            prev_i[chan] = cur_i[chan] = 0;
 
1261
    }
 
1262
    else {
 
1263
        if (!PyArg_ParseTuple(state,
 
1264
                        "iO!;audioop.ratecv: illegal state argument",
 
1265
                        &d, &PyTuple_Type, &samps))
 
1266
            goto exit;
 
1267
        if (PyTuple_Size(samps) != nchannels) {
 
1268
            PyErr_SetString(AudioopError,
 
1269
                            "illegal state argument");
 
1270
            goto exit;
 
1271
        }
 
1272
        for (chan = 0; chan < nchannels; chan++) {
 
1273
            if (!PyArg_ParseTuple(PyTuple_GetItem(samps, chan),
 
1274
                                  "ii:ratecv", &prev_i[chan],
 
1275
                                               &cur_i[chan]))
 
1276
                goto exit;
 
1277
        }
 
1278
    }
 
1279
 
 
1280
    /* str <- Space for the output buffer. */
 
1281
    if (len == 0)
 
1282
        str = PyBytes_FromStringAndSize(NULL, 0);
 
1283
    else {
 
1284
        /* There are len input frames, so we need (mathematically)
 
1285
           ceiling(len*outrate/inrate) output frames, and each frame
 
1286
           requires bytes_per_frame bytes.  Computing this
 
1287
           without spurious overflow is the challenge; we can
 
1288
           settle for a reasonable upper bound, though, in this
 
1289
           case ceiling(len/inrate) * outrate. */
 
1290
 
 
1291
        /* compute ceiling(len/inrate) without overflow */
 
1292
        Py_ssize_t q = len > 0 ? 1 + (len - 1) / inrate : 0;
 
1293
        if (outrate > PY_SSIZE_T_MAX / q / bytes_per_frame)
 
1294
            str = NULL;
 
1295
        else
 
1296
            str = PyBytes_FromStringAndSize(NULL,
 
1297
                                            q * outrate * bytes_per_frame);
 
1298
    }
 
1299
    if (str == NULL) {
 
1300
        PyErr_SetString(PyExc_MemoryError,
 
1301
            "not enough memory for output buffer");
 
1302
        goto exit;
 
1303
    }
 
1304
    ncp = PyBytes_AsString(str);
 
1305
    cp = view.buf;
 
1306
 
 
1307
    for (;;) {
 
1308
        while (d < 0) {
 
1309
            if (len == 0) {
 
1310
                samps = PyTuple_New(nchannels);
 
1311
                if (samps == NULL)
 
1312
                    goto exit;
 
1313
                for (chan = 0; chan < nchannels; chan++)
 
1314
                    PyTuple_SetItem(samps, chan,
 
1315
                        Py_BuildValue("(ii)",
 
1316
                                      prev_i[chan],
 
1317
                                      cur_i[chan]));
 
1318
                if (PyErr_Occurred())
 
1319
                    goto exit;
 
1320
                /* We have checked before that the length
 
1321
                 * of the string fits into int. */
 
1322
                len = (Py_ssize_t)(ncp - PyBytes_AsString(str));
 
1323
                rv = PyBytes_FromStringAndSize
 
1324
                    (PyBytes_AsString(str), len);
 
1325
                Py_DECREF(str);
 
1326
                str = rv;
 
1327
                if (str == NULL)
 
1328
                    goto exit;
 
1329
                rv = Py_BuildValue("(O(iO))", str, d, samps);
 
1330
                Py_DECREF(samps);
 
1331
                Py_DECREF(str);
 
1332
                goto exit; /* return rv */
 
1333
            }
 
1334
            for (chan = 0; chan < nchannels; chan++) {
 
1335
                prev_i[chan] = cur_i[chan];
 
1336
                cur_i[chan] = GETSAMPLE32(size, cp, 0);
 
1337
                cp += size;
 
1338
                /* implements a simple digital filter */
 
1339
                cur_i[chan] = (int)(
 
1340
                    ((double)weightA * (double)cur_i[chan] +
 
1341
                     (double)weightB * (double)prev_i[chan]) /
 
1342
                    ((double)weightA + (double)weightB));
 
1343
            }
 
1344
            len--;
 
1345
            d += outrate;
 
1346
        }
 
1347
        while (d >= 0) {
 
1348
            for (chan = 0; chan < nchannels; chan++) {
 
1349
                cur_o = (int)(((double)prev_i[chan] * (double)d +
 
1350
                         (double)cur_i[chan] * (double)(outrate - d)) /
 
1351
                    (double)outrate);
 
1352
                SETSAMPLE32(size, ncp, 0, cur_o);
 
1353
                ncp += size;
 
1354
            }
 
1355
            d -= inrate;
 
1356
        }
 
1357
    }
 
1358
  exit:
 
1359
    PyMem_Free(prev_i);
 
1360
    PyMem_Free(cur_i);
 
1361
  exit2:
 
1362
    PyBuffer_Release(&view);
 
1363
    return rv;
 
1364
}
 
1365
 
 
1366
static PyObject *
 
1367
audioop_lin2ulaw(PyObject *self, PyObject *args)
 
1368
{
 
1369
    Py_buffer view;
 
1370
    unsigned char *ncp;
 
1371
    Py_ssize_t i;
 
1372
    int size;
 
1373
    PyObject *rv = NULL;
 
1374
 
 
1375
    if (!PyArg_ParseTuple(args, "y*i:lin2ulaw",
 
1376
                          &view, &size))
 
1377
        return NULL;
 
1378
 
 
1379
    if (!audioop_check_parameters(view.len, size))
 
1380
        goto exit;
 
1381
 
 
1382
    rv = PyBytes_FromStringAndSize(NULL, view.len/size);
 
1383
    if (rv == NULL)
 
1384
        goto exit;
 
1385
    ncp = (unsigned char *)PyBytes_AsString(rv);
 
1386
 
 
1387
    for (i = 0; i < view.len; i += size) {
 
1388
        int val = GETSAMPLE32(size, view.buf, i);
 
1389
        *ncp++ = st_14linear2ulaw(val >> 18);
 
1390
    }
 
1391
  exit:
 
1392
    PyBuffer_Release(&view);
 
1393
    return rv;
 
1394
}
 
1395
 
 
1396
static PyObject *
 
1397
audioop_ulaw2lin(PyObject *self, PyObject *args)
 
1398
{
 
1399
    Py_buffer view;
 
1400
    unsigned char *cp;
 
1401
    signed char *ncp;
 
1402
    Py_ssize_t i;
 
1403
    int size;
 
1404
    PyObject *rv = NULL;
 
1405
 
 
1406
    if (!PyArg_ParseTuple(args, "y*i:ulaw2lin",
 
1407
                          &view, &size))
 
1408
        return NULL;
 
1409
 
 
1410
    if (!audioop_check_size(size))
 
1411
        goto exit;
 
1412
 
 
1413
    if (view.len > PY_SSIZE_T_MAX/size) {
 
1414
        PyErr_SetString(PyExc_MemoryError,
 
1415
                        "not enough memory for output buffer");
 
1416
        goto exit;
 
1417
    }
 
1418
    rv = PyBytes_FromStringAndSize(NULL, view.len*size);
 
1419
    if (rv == NULL)
 
1420
        goto exit;
 
1421
    ncp = (signed char *)PyBytes_AsString(rv);
 
1422
 
 
1423
    cp = view.buf;
 
1424
    for (i = 0; i < view.len*size; i += size) {
 
1425
        int val = st_ulaw2linear16(*cp++) << 16;
 
1426
        SETSAMPLE32(size, ncp, i, val);
 
1427
    }
 
1428
  exit:
 
1429
    PyBuffer_Release(&view);
 
1430
    return rv;
 
1431
}
 
1432
 
 
1433
static PyObject *
 
1434
audioop_lin2alaw(PyObject *self, PyObject *args)
 
1435
{
 
1436
    Py_buffer view;
 
1437
    unsigned char *ncp;
 
1438
    Py_ssize_t i;
 
1439
    int size;
 
1440
    PyObject *rv = NULL;
 
1441
 
 
1442
    if (!PyArg_ParseTuple(args, "y*i:lin2alaw",
 
1443
                          &view, &size))
 
1444
        return NULL;
 
1445
 
 
1446
    if (!audioop_check_parameters(view.len, size))
 
1447
        goto exit;
 
1448
 
 
1449
    rv = PyBytes_FromStringAndSize(NULL, view.len/size);
 
1450
    if (rv == NULL)
 
1451
        goto exit;
 
1452
    ncp = (unsigned char *)PyBytes_AsString(rv);
 
1453
 
 
1454
    for (i = 0; i < view.len; i += size) {
 
1455
        int val = GETSAMPLE32(size, view.buf, i);
 
1456
        *ncp++ = st_linear2alaw(val >> 19);
 
1457
    }
 
1458
  exit:
 
1459
    PyBuffer_Release(&view);
 
1460
    return rv;
 
1461
}
 
1462
 
 
1463
static PyObject *
 
1464
audioop_alaw2lin(PyObject *self, PyObject *args)
 
1465
{
 
1466
    Py_buffer view;
 
1467
    unsigned char *cp;
 
1468
    signed char *ncp;
 
1469
    Py_ssize_t i;
 
1470
    int size, val;
 
1471
    PyObject *rv = NULL;
 
1472
 
 
1473
    if (!PyArg_ParseTuple(args, "y*i:alaw2lin",
 
1474
                          &view, &size))
 
1475
        return NULL;
 
1476
 
 
1477
    if (!audioop_check_size(size))
 
1478
        goto exit;
 
1479
 
 
1480
    if (view.len > PY_SSIZE_T_MAX/size) {
 
1481
        PyErr_SetString(PyExc_MemoryError,
 
1482
                        "not enough memory for output buffer");
 
1483
        goto exit;
 
1484
    }
 
1485
    rv = PyBytes_FromStringAndSize(NULL, view.len*size);
 
1486
    if (rv == NULL)
 
1487
        goto exit;
 
1488
    ncp = (signed char *)PyBytes_AsString(rv);
 
1489
    cp = view.buf;
 
1490
 
 
1491
    for (i = 0; i < view.len*size; i += size) {
 
1492
        val = st_alaw2linear16(*cp++) << 16;
 
1493
        SETSAMPLE32(size, ncp, i, val);
 
1494
    }
 
1495
  exit:
 
1496
    PyBuffer_Release(&view);
 
1497
    return rv;
 
1498
}
 
1499
 
 
1500
static PyObject *
 
1501
audioop_lin2adpcm(PyObject *self, PyObject *args)
 
1502
{
 
1503
    Py_buffer view;
 
1504
    signed char *ncp;
 
1505
    Py_ssize_t i;
 
1506
    int size, step, valpred, delta,
 
1507
        index, sign, vpdiff, diff;
 
1508
    PyObject *rv = NULL, *state, *str;
 
1509
    int outputbuffer = 0, bufferstep;
 
1510
 
 
1511
    if (!PyArg_ParseTuple(args, "y*iO:lin2adpcm",
 
1512
                          &view, &size, &state))
 
1513
        return NULL;
 
1514
 
 
1515
    if (!audioop_check_parameters(view.len, size))
 
1516
        goto exit;
 
1517
 
 
1518
    str = PyBytes_FromStringAndSize(NULL, view.len/(size*2));
 
1519
    if (str == NULL)
 
1520
        goto exit;
 
1521
    ncp = (signed char *)PyBytes_AsString(str);
 
1522
 
 
1523
    /* Decode state, should have (value, step) */
 
1524
    if ( state == Py_None ) {
 
1525
        /* First time, it seems. Set defaults */
 
1526
        valpred = 0;
 
1527
        index = 0;
 
1528
    } else if (!PyArg_ParseTuple(state, "ii", &valpred, &index))
 
1529
        goto exit;
 
1530
 
 
1531
    step = stepsizeTable[index];
 
1532
    bufferstep = 1;
 
1533
 
 
1534
    for (i = 0; i < view.len; i += size) {
 
1535
        int val = GETSAMPLE32(size, view.buf, i) >> 16;
 
1536
 
 
1537
        /* Step 1 - compute difference with previous value */
 
1538
        if (val < valpred) {
 
1539
            diff = valpred - val;
 
1540
            sign = 8;
 
1541
        }
 
1542
        else {
 
1543
            diff = val - valpred;
 
1544
            sign = 0;
 
1545
        }
 
1546
 
 
1547
        /* Step 2 - Divide and clamp */
 
1548
        /* Note:
 
1549
        ** This code *approximately* computes:
 
1550
        **    delta = diff*4/step;
 
1551
        **    vpdiff = (delta+0.5)*step/4;
 
1552
        ** but in shift step bits are dropped. The net result of this
 
1553
        ** is that even if you have fast mul/div hardware you cannot
 
1554
        ** put it to good use since the fixup would be too expensive.
 
1555
        */
 
1556
        delta = 0;
 
1557
        vpdiff = (step >> 3);
 
1558
 
 
1559
        if ( diff >= step ) {
 
1560
            delta = 4;
 
1561
            diff -= step;
 
1562
            vpdiff += step;
 
1563
        }
 
1564
        step >>= 1;
 
1565
        if ( diff >= step  ) {
 
1566
            delta |= 2;
 
1567
            diff -= step;
 
1568
            vpdiff += step;
 
1569
        }
 
1570
        step >>= 1;
 
1571
        if ( diff >= step ) {
 
1572
            delta |= 1;
 
1573
            vpdiff += step;
 
1574
        }
 
1575
 
 
1576
        /* Step 3 - Update previous value */
 
1577
        if ( sign )
 
1578
            valpred -= vpdiff;
 
1579
        else
 
1580
            valpred += vpdiff;
 
1581
 
 
1582
        /* Step 4 - Clamp previous value to 16 bits */
 
1583
        if ( valpred > 32767 )
 
1584
            valpred = 32767;
 
1585
        else if ( valpred < -32768 )
 
1586
            valpred = -32768;
 
1587
 
 
1588
        /* Step 5 - Assemble value, update index and step values */
 
1589
        delta |= sign;
 
1590
 
 
1591
        index += indexTable[delta];
 
1592
        if ( index < 0 ) index = 0;
 
1593
        if ( index > 88 ) index = 88;
 
1594
        step = stepsizeTable[index];
 
1595
 
 
1596
        /* Step 6 - Output value */
 
1597
        if ( bufferstep ) {
 
1598
            outputbuffer = (delta << 4) & 0xf0;
 
1599
        } else {
 
1600
            *ncp++ = (delta & 0x0f) | outputbuffer;
 
1601
        }
 
1602
        bufferstep = !bufferstep;
 
1603
    }
 
1604
    rv = Py_BuildValue("(O(ii))", str, valpred, index);
 
1605
    Py_DECREF(str);
 
1606
  exit:
 
1607
    PyBuffer_Release(&view);
 
1608
    return rv;
 
1609
}
 
1610
 
 
1611
static PyObject *
 
1612
audioop_adpcm2lin(PyObject *self, PyObject *args)
 
1613
{
 
1614
    Py_buffer view;
 
1615
    signed char *cp;
 
1616
    signed char *ncp;
 
1617
    Py_ssize_t i, outlen;
 
1618
    int size, valpred, step, delta, index, sign, vpdiff;
 
1619
    PyObject *rv = NULL, *str, *state;
 
1620
    int inputbuffer = 0, bufferstep;
 
1621
 
 
1622
    if (!PyArg_ParseTuple(args, "y*iO:adpcm2lin",
 
1623
                          &view, &size, &state))
 
1624
        return NULL;
 
1625
 
 
1626
    if (!audioop_check_size(size))
 
1627
        goto exit;
 
1628
 
 
1629
    /* Decode state, should have (value, step) */
 
1630
    if ( state == Py_None ) {
 
1631
        /* First time, it seems. Set defaults */
 
1632
        valpred = 0;
 
1633
        index = 0;
 
1634
    } else if (!PyArg_ParseTuple(state, "ii", &valpred, &index))
 
1635
        goto exit;
 
1636
 
 
1637
    if (view.len > (PY_SSIZE_T_MAX/2)/size) {
 
1638
        PyErr_SetString(PyExc_MemoryError,
 
1639
                        "not enough memory for output buffer");
 
1640
        goto exit;
 
1641
    }
 
1642
    outlen = view.len*size*2;
 
1643
    str = PyBytes_FromStringAndSize(NULL, outlen);
 
1644
    if (str == NULL)
 
1645
        goto exit;
 
1646
    ncp = (signed char *)PyBytes_AsString(str);
 
1647
    cp = view.buf;
 
1648
 
 
1649
    step = stepsizeTable[index];
 
1650
    bufferstep = 0;
 
1651
 
 
1652
    for (i = 0; i < outlen; i += size) {
 
1653
        /* Step 1 - get the delta value and compute next index */
 
1654
        if ( bufferstep ) {
 
1655
            delta = inputbuffer & 0xf;
 
1656
        } else {
 
1657
            inputbuffer = *cp++;
 
1658
            delta = (inputbuffer >> 4) & 0xf;
 
1659
        }
 
1660
 
 
1661
        bufferstep = !bufferstep;
 
1662
 
 
1663
        /* Step 2 - Find new index value (for later) */
 
1664
        index += indexTable[delta];
 
1665
        if ( index < 0 ) index = 0;
 
1666
        if ( index > 88 ) index = 88;
 
1667
 
 
1668
        /* Step 3 - Separate sign and magnitude */
 
1669
        sign = delta & 8;
 
1670
        delta = delta & 7;
 
1671
 
 
1672
        /* Step 4 - Compute difference and new predicted value */
 
1673
        /*
 
1674
        ** Computes 'vpdiff = (delta+0.5)*step/4', but see comment
 
1675
        ** in adpcm_coder.
 
1676
        */
 
1677
        vpdiff = step >> 3;
 
1678
        if ( delta & 4 ) vpdiff += step;
 
1679
        if ( delta & 2 ) vpdiff += step>>1;
 
1680
        if ( delta & 1 ) vpdiff += step>>2;
 
1681
 
 
1682
        if ( sign )
 
1683
            valpred -= vpdiff;
 
1684
        else
 
1685
            valpred += vpdiff;
 
1686
 
 
1687
        /* Step 5 - clamp output value */
 
1688
        if ( valpred > 32767 )
 
1689
            valpred = 32767;
 
1690
        else if ( valpred < -32768 )
 
1691
            valpred = -32768;
 
1692
 
 
1693
        /* Step 6 - Update step value */
 
1694
        step = stepsizeTable[index];
 
1695
 
 
1696
        /* Step 6 - Output value */
 
1697
        SETSAMPLE32(size, ncp, i, valpred << 16);
 
1698
    }
 
1699
 
 
1700
    rv = Py_BuildValue("(O(ii))", str, valpred, index);
 
1701
    Py_DECREF(str);
 
1702
  exit:
 
1703
    PyBuffer_Release(&view);
 
1704
    return rv;
 
1705
}
 
1706
 
 
1707
static PyMethodDef audioop_methods[] = {
 
1708
    { "max", audioop_max, METH_VARARGS },
 
1709
    { "minmax", audioop_minmax, METH_VARARGS },
 
1710
    { "avg", audioop_avg, METH_VARARGS },
 
1711
    { "maxpp", audioop_maxpp, METH_VARARGS },
 
1712
    { "avgpp", audioop_avgpp, METH_VARARGS },
 
1713
    { "rms", audioop_rms, METH_VARARGS },
 
1714
    { "findfit", audioop_findfit, METH_VARARGS },
 
1715
    { "findmax", audioop_findmax, METH_VARARGS },
 
1716
    { "findfactor", audioop_findfactor, METH_VARARGS },
 
1717
    { "cross", audioop_cross, METH_VARARGS },
 
1718
    { "mul", audioop_mul, METH_VARARGS },
 
1719
    { "add", audioop_add, METH_VARARGS },
 
1720
    { "bias", audioop_bias, METH_VARARGS },
 
1721
    { "ulaw2lin", audioop_ulaw2lin, METH_VARARGS },
 
1722
    { "lin2ulaw", audioop_lin2ulaw, METH_VARARGS },
 
1723
    { "alaw2lin", audioop_alaw2lin, METH_VARARGS },
 
1724
    { "lin2alaw", audioop_lin2alaw, METH_VARARGS },
 
1725
    { "lin2lin", audioop_lin2lin, METH_VARARGS },
 
1726
    { "adpcm2lin", audioop_adpcm2lin, METH_VARARGS },
 
1727
    { "lin2adpcm", audioop_lin2adpcm, METH_VARARGS },
 
1728
    { "tomono", audioop_tomono, METH_VARARGS },
 
1729
    { "tostereo", audioop_tostereo, METH_VARARGS },
 
1730
    { "getsample", audioop_getsample, METH_VARARGS },
 
1731
    { "reverse", audioop_reverse, METH_VARARGS },
 
1732
    { "byteswap", audioop_byteswap, METH_VARARGS },
 
1733
    { "ratecv", audioop_ratecv, METH_VARARGS },
 
1734
    { 0,          0 }
 
1735
};
 
1736
 
 
1737
 
 
1738
static struct PyModuleDef audioopmodule = {
 
1739
    PyModuleDef_HEAD_INIT,
 
1740
    "audioop",
 
1741
    NULL,
 
1742
    -1,
 
1743
    audioop_methods,
 
1744
    NULL,
 
1745
    NULL,
 
1746
    NULL,
 
1747
    NULL
 
1748
};
 
1749
 
 
1750
PyMODINIT_FUNC
 
1751
PyInit_audioop(void)
 
1752
{
 
1753
    PyObject *m, *d;
 
1754
    m = PyModule_Create(&audioopmodule);
 
1755
    if (m == NULL)
 
1756
        return NULL;
 
1757
    d = PyModule_GetDict(m);
 
1758
    if (d == NULL)
 
1759
        return NULL;
 
1760
    AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
 
1761
    if (AudioopError != NULL)
 
1762
         PyDict_SetItemString(d,"error",AudioopError);
 
1763
    return m;
 
1764
}