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