blob: da12bd17b5a60d6d21db89cd119dfc165d9e4955 [file] [log] [blame]
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(audioop_getsample__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08006"getsample($module, fragment, width, index, /)\n"
7"--\n"
8"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02009"Return the value of sample index from the fragment.");
10
11#define AUDIOOP_GETSAMPLE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020012 {"getsample", (PyCFunction)(void(*)(void))audioop_getsample, METH_FASTCALL, audioop_getsample__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020013
14static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030015audioop_getsample_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -040016 Py_ssize_t index);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020017
18static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020019audioop_getsample(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020020{
21 PyObject *return_value = NULL;
22 Py_buffer fragment = {NULL, NULL};
23 int width;
24 Py_ssize_t index;
25
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020026 if (!_PyArg_CheckPositional("getsample", nargs, 3, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +010027 goto exit;
28 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020029 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
30 goto exit;
31 }
32 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020033 _PyArg_BadArgument("getsample", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020034 goto exit;
35 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020036 width = _PyLong_AsInt(args[1]);
37 if (width == -1 && PyErr_Occurred()) {
38 goto exit;
39 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020040 {
41 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +030042 PyObject *iobj = _PyNumber_Index(args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020043 if (iobj != NULL) {
44 ival = PyLong_AsSsize_t(iobj);
45 Py_DECREF(iobj);
46 }
47 if (ival == -1 && PyErr_Occurred()) {
48 goto exit;
49 }
50 index = ival;
51 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020052 return_value = audioop_getsample_impl(module, &fragment, width, index);
53
54exit:
55 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030056 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020057 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030058 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020059
60 return return_value;
61}
62
63PyDoc_STRVAR(audioop_max__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080064"max($module, fragment, width, /)\n"
65"--\n"
66"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020067"Return the maximum of the absolute value of all samples in a fragment.");
68
69#define AUDIOOP_MAX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020070 {"max", (PyCFunction)(void(*)(void))audioop_max, METH_FASTCALL, audioop_max__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020071
72static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030073audioop_max_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020074
75static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020076audioop_max(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020077{
78 PyObject *return_value = NULL;
79 Py_buffer fragment = {NULL, NULL};
80 int width;
81
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020082 if (!_PyArg_CheckPositional("max", nargs, 2, 2)) {
83 goto exit;
84 }
85 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
86 goto exit;
87 }
88 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020089 _PyArg_BadArgument("max", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020090 goto exit;
91 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +020092 width = _PyLong_AsInt(args[1]);
93 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +010094 goto exit;
95 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +020096 return_value = audioop_max_impl(module, &fragment, width);
97
98exit:
99 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300100 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200101 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300102 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200103
104 return return_value;
105}
106
107PyDoc_STRVAR(audioop_minmax__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800108"minmax($module, fragment, width, /)\n"
109"--\n"
110"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200111"Return the minimum and maximum values of all samples in the sound fragment.");
112
113#define AUDIOOP_MINMAX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200114 {"minmax", (PyCFunction)(void(*)(void))audioop_minmax, METH_FASTCALL, audioop_minmax__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200115
116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300117audioop_minmax_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200118
119static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200120audioop_minmax(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200121{
122 PyObject *return_value = NULL;
123 Py_buffer fragment = {NULL, NULL};
124 int width;
125
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200126 if (!_PyArg_CheckPositional("minmax", nargs, 2, 2)) {
127 goto exit;
128 }
129 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
130 goto exit;
131 }
132 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200133 _PyArg_BadArgument("minmax", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200134 goto exit;
135 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200136 width = _PyLong_AsInt(args[1]);
137 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100138 goto exit;
139 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200140 return_value = audioop_minmax_impl(module, &fragment, width);
141
142exit:
143 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300144 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200145 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300146 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200147
148 return return_value;
149}
150
151PyDoc_STRVAR(audioop_avg__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800152"avg($module, fragment, width, /)\n"
153"--\n"
154"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200155"Return the average over all samples in the fragment.");
156
157#define AUDIOOP_AVG_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200158 {"avg", (PyCFunction)(void(*)(void))audioop_avg, METH_FASTCALL, audioop_avg__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200159
160static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300161audioop_avg_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200162
163static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200164audioop_avg(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200165{
166 PyObject *return_value = NULL;
167 Py_buffer fragment = {NULL, NULL};
168 int width;
169
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200170 if (!_PyArg_CheckPositional("avg", nargs, 2, 2)) {
171 goto exit;
172 }
173 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
174 goto exit;
175 }
176 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200177 _PyArg_BadArgument("avg", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200178 goto exit;
179 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200180 width = _PyLong_AsInt(args[1]);
181 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100182 goto exit;
183 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200184 return_value = audioop_avg_impl(module, &fragment, width);
185
186exit:
187 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300188 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200189 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300190 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200191
192 return return_value;
193}
194
195PyDoc_STRVAR(audioop_rms__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800196"rms($module, fragment, width, /)\n"
197"--\n"
198"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200199"Return the root-mean-square of the fragment, i.e. sqrt(sum(S_i^2)/n).");
200
201#define AUDIOOP_RMS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200202 {"rms", (PyCFunction)(void(*)(void))audioop_rms, METH_FASTCALL, audioop_rms__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200203
204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300205audioop_rms_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200206
207static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200208audioop_rms(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200209{
210 PyObject *return_value = NULL;
211 Py_buffer fragment = {NULL, NULL};
212 int width;
213
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200214 if (!_PyArg_CheckPositional("rms", nargs, 2, 2)) {
215 goto exit;
216 }
217 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
218 goto exit;
219 }
220 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200221 _PyArg_BadArgument("rms", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200222 goto exit;
223 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200224 width = _PyLong_AsInt(args[1]);
225 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100226 goto exit;
227 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200228 return_value = audioop_rms_impl(module, &fragment, width);
229
230exit:
231 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300232 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200233 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300234 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200235
236 return return_value;
237}
238
239PyDoc_STRVAR(audioop_findfit__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800240"findfit($module, fragment, reference, /)\n"
241"--\n"
242"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200243"Try to match reference as well as possible to a portion of fragment.");
244
245#define AUDIOOP_FINDFIT_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200246 {"findfit", (PyCFunction)(void(*)(void))audioop_findfit, METH_FASTCALL, audioop_findfit__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200247
248static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300249audioop_findfit_impl(PyObject *module, Py_buffer *fragment,
Larry Hastings89964c42015-04-14 18:07:59 -0400250 Py_buffer *reference);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200251
252static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200253audioop_findfit(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200254{
255 PyObject *return_value = NULL;
256 Py_buffer fragment = {NULL, NULL};
257 Py_buffer reference = {NULL, NULL};
258
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200259 if (!_PyArg_CheckPositional("findfit", nargs, 2, 2)) {
260 goto exit;
261 }
262 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
263 goto exit;
264 }
265 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200266 _PyArg_BadArgument("findfit", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200267 goto exit;
268 }
269 if (PyObject_GetBuffer(args[1], &reference, PyBUF_SIMPLE) != 0) {
270 goto exit;
271 }
272 if (!PyBuffer_IsContiguous(&reference, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200273 _PyArg_BadArgument("findfit", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100274 goto exit;
275 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200276 return_value = audioop_findfit_impl(module, &fragment, &reference);
277
278exit:
279 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300280 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200281 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300282 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200283 /* Cleanup for reference */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300284 if (reference.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200285 PyBuffer_Release(&reference);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300286 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200287
288 return return_value;
289}
290
291PyDoc_STRVAR(audioop_findfactor__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800292"findfactor($module, fragment, reference, /)\n"
293"--\n"
294"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200295"Return a factor F such that rms(add(fragment, mul(reference, -F))) is minimal.");
296
297#define AUDIOOP_FINDFACTOR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200298 {"findfactor", (PyCFunction)(void(*)(void))audioop_findfactor, METH_FASTCALL, audioop_findfactor__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200299
300static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300301audioop_findfactor_impl(PyObject *module, Py_buffer *fragment,
Larry Hastings89964c42015-04-14 18:07:59 -0400302 Py_buffer *reference);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200303
304static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200305audioop_findfactor(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200306{
307 PyObject *return_value = NULL;
308 Py_buffer fragment = {NULL, NULL};
309 Py_buffer reference = {NULL, NULL};
310
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200311 if (!_PyArg_CheckPositional("findfactor", nargs, 2, 2)) {
312 goto exit;
313 }
314 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
315 goto exit;
316 }
317 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200318 _PyArg_BadArgument("findfactor", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200319 goto exit;
320 }
321 if (PyObject_GetBuffer(args[1], &reference, PyBUF_SIMPLE) != 0) {
322 goto exit;
323 }
324 if (!PyBuffer_IsContiguous(&reference, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200325 _PyArg_BadArgument("findfactor", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100326 goto exit;
327 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200328 return_value = audioop_findfactor_impl(module, &fragment, &reference);
329
330exit:
331 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300332 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200333 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300334 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200335 /* Cleanup for reference */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300336 if (reference.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200337 PyBuffer_Release(&reference);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300338 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200339
340 return return_value;
341}
342
343PyDoc_STRVAR(audioop_findmax__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800344"findmax($module, fragment, length, /)\n"
345"--\n"
346"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200347"Search fragment for a slice of specified number of samples with maximum energy.");
348
349#define AUDIOOP_FINDMAX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200350 {"findmax", (PyCFunction)(void(*)(void))audioop_findmax, METH_FASTCALL, audioop_findmax__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200351
352static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300353audioop_findmax_impl(PyObject *module, Py_buffer *fragment,
Larry Hastings89964c42015-04-14 18:07:59 -0400354 Py_ssize_t length);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200355
356static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200357audioop_findmax(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200358{
359 PyObject *return_value = NULL;
360 Py_buffer fragment = {NULL, NULL};
361 Py_ssize_t length;
362
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200363 if (!_PyArg_CheckPositional("findmax", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100364 goto exit;
365 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200366 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
367 goto exit;
368 }
369 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200370 _PyArg_BadArgument("findmax", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200371 goto exit;
372 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200373 {
374 Py_ssize_t ival = -1;
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300375 PyObject *iobj = _PyNumber_Index(args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200376 if (iobj != NULL) {
377 ival = PyLong_AsSsize_t(iobj);
378 Py_DECREF(iobj);
379 }
380 if (ival == -1 && PyErr_Occurred()) {
381 goto exit;
382 }
383 length = ival;
384 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200385 return_value = audioop_findmax_impl(module, &fragment, length);
386
387exit:
388 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300389 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200390 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300391 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200392
393 return return_value;
394}
395
396PyDoc_STRVAR(audioop_avgpp__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800397"avgpp($module, fragment, width, /)\n"
398"--\n"
399"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200400"Return the average peak-peak value over all samples in the fragment.");
401
402#define AUDIOOP_AVGPP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200403 {"avgpp", (PyCFunction)(void(*)(void))audioop_avgpp, METH_FASTCALL, audioop_avgpp__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200404
405static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300406audioop_avgpp_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200407
408static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200409audioop_avgpp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200410{
411 PyObject *return_value = NULL;
412 Py_buffer fragment = {NULL, NULL};
413 int width;
414
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200415 if (!_PyArg_CheckPositional("avgpp", nargs, 2, 2)) {
416 goto exit;
417 }
418 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
419 goto exit;
420 }
421 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200422 _PyArg_BadArgument("avgpp", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200423 goto exit;
424 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200425 width = _PyLong_AsInt(args[1]);
426 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100427 goto exit;
428 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200429 return_value = audioop_avgpp_impl(module, &fragment, width);
430
431exit:
432 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300433 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200434 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300435 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200436
437 return return_value;
438}
439
440PyDoc_STRVAR(audioop_maxpp__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800441"maxpp($module, fragment, width, /)\n"
442"--\n"
443"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200444"Return the maximum peak-peak value in the sound fragment.");
445
446#define AUDIOOP_MAXPP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200447 {"maxpp", (PyCFunction)(void(*)(void))audioop_maxpp, METH_FASTCALL, audioop_maxpp__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200448
449static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300450audioop_maxpp_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200451
452static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200453audioop_maxpp(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200454{
455 PyObject *return_value = NULL;
456 Py_buffer fragment = {NULL, NULL};
457 int width;
458
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200459 if (!_PyArg_CheckPositional("maxpp", nargs, 2, 2)) {
460 goto exit;
461 }
462 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
463 goto exit;
464 }
465 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200466 _PyArg_BadArgument("maxpp", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200467 goto exit;
468 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200469 width = _PyLong_AsInt(args[1]);
470 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100471 goto exit;
472 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200473 return_value = audioop_maxpp_impl(module, &fragment, width);
474
475exit:
476 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300477 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200478 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300479 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200480
481 return return_value;
482}
483
484PyDoc_STRVAR(audioop_cross__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800485"cross($module, fragment, width, /)\n"
486"--\n"
487"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200488"Return the number of zero crossings in the fragment passed as an argument.");
489
490#define AUDIOOP_CROSS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200491 {"cross", (PyCFunction)(void(*)(void))audioop_cross, METH_FASTCALL, audioop_cross__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200492
493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300494audioop_cross_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200495
496static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200497audioop_cross(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200498{
499 PyObject *return_value = NULL;
500 Py_buffer fragment = {NULL, NULL};
501 int width;
502
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200503 if (!_PyArg_CheckPositional("cross", nargs, 2, 2)) {
504 goto exit;
505 }
506 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
507 goto exit;
508 }
509 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200510 _PyArg_BadArgument("cross", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200511 goto exit;
512 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200513 width = _PyLong_AsInt(args[1]);
514 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100515 goto exit;
516 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200517 return_value = audioop_cross_impl(module, &fragment, width);
518
519exit:
520 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300521 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200522 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300523 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200524
525 return return_value;
526}
527
528PyDoc_STRVAR(audioop_mul__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800529"mul($module, fragment, width, factor, /)\n"
530"--\n"
531"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200532"Return a fragment that has all samples in the original fragment multiplied by the floating-point value factor.");
533
534#define AUDIOOP_MUL_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200535 {"mul", (PyCFunction)(void(*)(void))audioop_mul, METH_FASTCALL, audioop_mul__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200536
537static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300538audioop_mul_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -0400539 double factor);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200540
541static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200542audioop_mul(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200543{
544 PyObject *return_value = NULL;
545 Py_buffer fragment = {NULL, NULL};
546 int width;
547 double factor;
548
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200549 if (!_PyArg_CheckPositional("mul", nargs, 3, 3)) {
550 goto exit;
551 }
552 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
553 goto exit;
554 }
555 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200556 _PyArg_BadArgument("mul", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200557 goto exit;
558 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200559 width = _PyLong_AsInt(args[1]);
560 if (width == -1 && PyErr_Occurred()) {
561 goto exit;
562 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700563 if (PyFloat_CheckExact(args[2])) {
564 factor = PyFloat_AS_DOUBLE(args[2]);
565 }
566 else
567 {
568 factor = PyFloat_AsDouble(args[2]);
569 if (factor == -1.0 && PyErr_Occurred()) {
570 goto exit;
571 }
Victor Stinner259f0e42017-01-17 01:35:17 +0100572 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200573 return_value = audioop_mul_impl(module, &fragment, width, factor);
574
575exit:
576 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300577 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200578 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300579 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200580
581 return return_value;
582}
583
584PyDoc_STRVAR(audioop_tomono__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800585"tomono($module, fragment, width, lfactor, rfactor, /)\n"
586"--\n"
587"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200588"Convert a stereo fragment to a mono fragment.");
589
590#define AUDIOOP_TOMONO_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200591 {"tomono", (PyCFunction)(void(*)(void))audioop_tomono, METH_FASTCALL, audioop_tomono__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200592
593static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300594audioop_tomono_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -0400595 double lfactor, double rfactor);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200596
597static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200598audioop_tomono(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200599{
600 PyObject *return_value = NULL;
601 Py_buffer fragment = {NULL, NULL};
602 int width;
603 double lfactor;
604 double rfactor;
605
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200606 if (!_PyArg_CheckPositional("tomono", nargs, 4, 4)) {
607 goto exit;
608 }
609 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
610 goto exit;
611 }
612 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200613 _PyArg_BadArgument("tomono", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200614 goto exit;
615 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200616 width = _PyLong_AsInt(args[1]);
617 if (width == -1 && PyErr_Occurred()) {
618 goto exit;
619 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700620 if (PyFloat_CheckExact(args[2])) {
621 lfactor = PyFloat_AS_DOUBLE(args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200622 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700623 else
624 {
625 lfactor = PyFloat_AsDouble(args[2]);
626 if (lfactor == -1.0 && PyErr_Occurred()) {
627 goto exit;
628 }
629 }
630 if (PyFloat_CheckExact(args[3])) {
631 rfactor = PyFloat_AS_DOUBLE(args[3]);
632 }
633 else
634 {
635 rfactor = PyFloat_AsDouble(args[3]);
636 if (rfactor == -1.0 && PyErr_Occurred()) {
637 goto exit;
638 }
Victor Stinner259f0e42017-01-17 01:35:17 +0100639 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200640 return_value = audioop_tomono_impl(module, &fragment, width, lfactor, rfactor);
641
642exit:
643 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300644 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200645 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300646 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200647
648 return return_value;
649}
650
651PyDoc_STRVAR(audioop_tostereo__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800652"tostereo($module, fragment, width, lfactor, rfactor, /)\n"
653"--\n"
654"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200655"Generate a stereo fragment from a mono fragment.");
656
657#define AUDIOOP_TOSTEREO_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200658 {"tostereo", (PyCFunction)(void(*)(void))audioop_tostereo, METH_FASTCALL, audioop_tostereo__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200659
660static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300661audioop_tostereo_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -0400662 double lfactor, double rfactor);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200663
664static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200665audioop_tostereo(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200666{
667 PyObject *return_value = NULL;
668 Py_buffer fragment = {NULL, NULL};
669 int width;
670 double lfactor;
671 double rfactor;
672
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200673 if (!_PyArg_CheckPositional("tostereo", nargs, 4, 4)) {
674 goto exit;
675 }
676 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
677 goto exit;
678 }
679 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200680 _PyArg_BadArgument("tostereo", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200681 goto exit;
682 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200683 width = _PyLong_AsInt(args[1]);
684 if (width == -1 && PyErr_Occurred()) {
685 goto exit;
686 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700687 if (PyFloat_CheckExact(args[2])) {
688 lfactor = PyFloat_AS_DOUBLE(args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200689 }
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700690 else
691 {
692 lfactor = PyFloat_AsDouble(args[2]);
693 if (lfactor == -1.0 && PyErr_Occurred()) {
694 goto exit;
695 }
696 }
697 if (PyFloat_CheckExact(args[3])) {
698 rfactor = PyFloat_AS_DOUBLE(args[3]);
699 }
700 else
701 {
702 rfactor = PyFloat_AsDouble(args[3]);
703 if (rfactor == -1.0 && PyErr_Occurred()) {
704 goto exit;
705 }
Victor Stinner259f0e42017-01-17 01:35:17 +0100706 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200707 return_value = audioop_tostereo_impl(module, &fragment, width, lfactor, rfactor);
708
709exit:
710 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300711 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200712 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300713 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200714
715 return return_value;
716}
717
718PyDoc_STRVAR(audioop_add__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800719"add($module, fragment1, fragment2, width, /)\n"
720"--\n"
721"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200722"Return a fragment which is the addition of the two samples passed as parameters.");
723
724#define AUDIOOP_ADD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200725 {"add", (PyCFunction)(void(*)(void))audioop_add, METH_FASTCALL, audioop_add__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200726
727static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300728audioop_add_impl(PyObject *module, Py_buffer *fragment1,
Larry Hastings89964c42015-04-14 18:07:59 -0400729 Py_buffer *fragment2, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200730
731static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200732audioop_add(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200733{
734 PyObject *return_value = NULL;
735 Py_buffer fragment1 = {NULL, NULL};
736 Py_buffer fragment2 = {NULL, NULL};
737 int width;
738
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200739 if (!_PyArg_CheckPositional("add", nargs, 3, 3)) {
740 goto exit;
741 }
742 if (PyObject_GetBuffer(args[0], &fragment1, PyBUF_SIMPLE) != 0) {
743 goto exit;
744 }
745 if (!PyBuffer_IsContiguous(&fragment1, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200746 _PyArg_BadArgument("add", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200747 goto exit;
748 }
749 if (PyObject_GetBuffer(args[1], &fragment2, PyBUF_SIMPLE) != 0) {
750 goto exit;
751 }
752 if (!PyBuffer_IsContiguous(&fragment2, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200753 _PyArg_BadArgument("add", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200754 goto exit;
755 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200756 width = _PyLong_AsInt(args[2]);
757 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100758 goto exit;
759 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200760 return_value = audioop_add_impl(module, &fragment1, &fragment2, width);
761
762exit:
763 /* Cleanup for fragment1 */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300764 if (fragment1.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200765 PyBuffer_Release(&fragment1);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300766 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200767 /* Cleanup for fragment2 */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300768 if (fragment2.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200769 PyBuffer_Release(&fragment2);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300770 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200771
772 return return_value;
773}
774
775PyDoc_STRVAR(audioop_bias__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800776"bias($module, fragment, width, bias, /)\n"
777"--\n"
778"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200779"Return a fragment that is the original fragment with a bias added to each sample.");
780
781#define AUDIOOP_BIAS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200782 {"bias", (PyCFunction)(void(*)(void))audioop_bias, METH_FASTCALL, audioop_bias__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200783
784static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300785audioop_bias_impl(PyObject *module, Py_buffer *fragment, int width, int bias);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200786
787static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200788audioop_bias(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200789{
790 PyObject *return_value = NULL;
791 Py_buffer fragment = {NULL, NULL};
792 int width;
793 int bias;
794
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200795 if (!_PyArg_CheckPositional("bias", nargs, 3, 3)) {
796 goto exit;
797 }
798 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
799 goto exit;
800 }
801 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200802 _PyArg_BadArgument("bias", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200803 goto exit;
804 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200805 width = _PyLong_AsInt(args[1]);
806 if (width == -1 && PyErr_Occurred()) {
807 goto exit;
808 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200809 bias = _PyLong_AsInt(args[2]);
810 if (bias == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100811 goto exit;
812 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200813 return_value = audioop_bias_impl(module, &fragment, width, bias);
814
815exit:
816 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300817 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200818 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300819 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200820
821 return return_value;
822}
823
824PyDoc_STRVAR(audioop_reverse__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800825"reverse($module, fragment, width, /)\n"
826"--\n"
827"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200828"Reverse the samples in a fragment and returns the modified fragment.");
829
830#define AUDIOOP_REVERSE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200831 {"reverse", (PyCFunction)(void(*)(void))audioop_reverse, METH_FASTCALL, audioop_reverse__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200832
833static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300834audioop_reverse_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200835
836static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200837audioop_reverse(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200838{
839 PyObject *return_value = NULL;
840 Py_buffer fragment = {NULL, NULL};
841 int width;
842
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200843 if (!_PyArg_CheckPositional("reverse", nargs, 2, 2)) {
844 goto exit;
845 }
846 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
847 goto exit;
848 }
849 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200850 _PyArg_BadArgument("reverse", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200851 goto exit;
852 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200853 width = _PyLong_AsInt(args[1]);
854 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100855 goto exit;
856 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200857 return_value = audioop_reverse_impl(module, &fragment, width);
858
859exit:
860 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300861 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200862 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300863 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200864
865 return return_value;
866}
867
868PyDoc_STRVAR(audioop_byteswap__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800869"byteswap($module, fragment, width, /)\n"
870"--\n"
871"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200872"Convert big-endian samples to little-endian and vice versa.");
873
874#define AUDIOOP_BYTESWAP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200875 {"byteswap", (PyCFunction)(void(*)(void))audioop_byteswap, METH_FASTCALL, audioop_byteswap__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200876
877static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300878audioop_byteswap_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200879
880static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200881audioop_byteswap(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200882{
883 PyObject *return_value = NULL;
884 Py_buffer fragment = {NULL, NULL};
885 int width;
886
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200887 if (!_PyArg_CheckPositional("byteswap", nargs, 2, 2)) {
888 goto exit;
889 }
890 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
891 goto exit;
892 }
893 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200894 _PyArg_BadArgument("byteswap", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200895 goto exit;
896 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200897 width = _PyLong_AsInt(args[1]);
898 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100899 goto exit;
900 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200901 return_value = audioop_byteswap_impl(module, &fragment, width);
902
903exit:
904 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300905 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200906 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300907 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200908
909 return return_value;
910}
911
912PyDoc_STRVAR(audioop_lin2lin__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800913"lin2lin($module, fragment, width, newwidth, /)\n"
914"--\n"
915"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200916"Convert samples between 1-, 2-, 3- and 4-byte formats.");
917
918#define AUDIOOP_LIN2LIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200919 {"lin2lin", (PyCFunction)(void(*)(void))audioop_lin2lin, METH_FASTCALL, audioop_lin2lin__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200920
921static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300922audioop_lin2lin_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -0400923 int newwidth);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200924
925static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200926audioop_lin2lin(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200927{
928 PyObject *return_value = NULL;
929 Py_buffer fragment = {NULL, NULL};
930 int width;
931 int newwidth;
932
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200933 if (!_PyArg_CheckPositional("lin2lin", nargs, 3, 3)) {
934 goto exit;
935 }
936 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
937 goto exit;
938 }
939 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200940 _PyArg_BadArgument("lin2lin", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200941 goto exit;
942 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200943 width = _PyLong_AsInt(args[1]);
944 if (width == -1 && PyErr_Occurred()) {
945 goto exit;
946 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200947 newwidth = _PyLong_AsInt(args[2]);
948 if (newwidth == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100949 goto exit;
950 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200951 return_value = audioop_lin2lin_impl(module, &fragment, width, newwidth);
952
953exit:
954 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300955 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200956 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300957 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200958
959 return return_value;
960}
961
962PyDoc_STRVAR(audioop_ratecv__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800963"ratecv($module, fragment, width, nchannels, inrate, outrate, state,\n"
964" weightA=1, weightB=0, /)\n"
965"--\n"
966"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200967"Convert the frame rate of the input fragment.");
968
969#define AUDIOOP_RATECV_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200970 {"ratecv", (PyCFunction)(void(*)(void))audioop_ratecv, METH_FASTCALL, audioop_ratecv__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200971
972static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300973audioop_ratecv_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -0400974 int nchannels, int inrate, int outrate, PyObject *state,
975 int weightA, int weightB);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200976
977static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200978audioop_ratecv(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +0200979{
980 PyObject *return_value = NULL;
981 Py_buffer fragment = {NULL, NULL};
982 int width;
983 int nchannels;
984 int inrate;
985 int outrate;
986 PyObject *state;
987 int weightA = 1;
988 int weightB = 0;
989
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200990 if (!_PyArg_CheckPositional("ratecv", nargs, 6, 8)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100991 goto exit;
992 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200993 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
994 goto exit;
995 }
996 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200997 _PyArg_BadArgument("ratecv", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200998 goto exit;
999 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001000 width = _PyLong_AsInt(args[1]);
1001 if (width == -1 && PyErr_Occurred()) {
1002 goto exit;
1003 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001004 nchannels = _PyLong_AsInt(args[2]);
1005 if (nchannels == -1 && PyErr_Occurred()) {
1006 goto exit;
1007 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001008 inrate = _PyLong_AsInt(args[3]);
1009 if (inrate == -1 && PyErr_Occurred()) {
1010 goto exit;
1011 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001012 outrate = _PyLong_AsInt(args[4]);
1013 if (outrate == -1 && PyErr_Occurred()) {
1014 goto exit;
1015 }
1016 state = args[5];
1017 if (nargs < 7) {
1018 goto skip_optional;
1019 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001020 weightA = _PyLong_AsInt(args[6]);
1021 if (weightA == -1 && PyErr_Occurred()) {
1022 goto exit;
1023 }
1024 if (nargs < 8) {
1025 goto skip_optional;
1026 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001027 weightB = _PyLong_AsInt(args[7]);
1028 if (weightB == -1 && PyErr_Occurred()) {
1029 goto exit;
1030 }
1031skip_optional:
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001032 return_value = audioop_ratecv_impl(module, &fragment, width, nchannels, inrate, outrate, state, weightA, weightB);
1033
1034exit:
1035 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001036 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001037 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001038 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001039
1040 return return_value;
1041}
1042
1043PyDoc_STRVAR(audioop_lin2ulaw__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001044"lin2ulaw($module, fragment, width, /)\n"
1045"--\n"
1046"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001047"Convert samples in the audio fragment to u-LAW encoding.");
1048
1049#define AUDIOOP_LIN2ULAW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001050 {"lin2ulaw", (PyCFunction)(void(*)(void))audioop_lin2ulaw, METH_FASTCALL, audioop_lin2ulaw__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001051
1052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001053audioop_lin2ulaw_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001054
1055static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001056audioop_lin2ulaw(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001057{
1058 PyObject *return_value = NULL;
1059 Py_buffer fragment = {NULL, NULL};
1060 int width;
1061
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001062 if (!_PyArg_CheckPositional("lin2ulaw", nargs, 2, 2)) {
1063 goto exit;
1064 }
1065 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
1066 goto exit;
1067 }
1068 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001069 _PyArg_BadArgument("lin2ulaw", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001070 goto exit;
1071 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001072 width = _PyLong_AsInt(args[1]);
1073 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001074 goto exit;
1075 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001076 return_value = audioop_lin2ulaw_impl(module, &fragment, width);
1077
1078exit:
1079 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001080 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001081 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001082 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001083
1084 return return_value;
1085}
1086
1087PyDoc_STRVAR(audioop_ulaw2lin__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001088"ulaw2lin($module, fragment, width, /)\n"
1089"--\n"
1090"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001091"Convert sound fragments in u-LAW encoding to linearly encoded sound fragments.");
1092
1093#define AUDIOOP_ULAW2LIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001094 {"ulaw2lin", (PyCFunction)(void(*)(void))audioop_ulaw2lin, METH_FASTCALL, audioop_ulaw2lin__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001095
1096static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001097audioop_ulaw2lin_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001098
1099static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001100audioop_ulaw2lin(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001101{
1102 PyObject *return_value = NULL;
1103 Py_buffer fragment = {NULL, NULL};
1104 int width;
1105
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001106 if (!_PyArg_CheckPositional("ulaw2lin", nargs, 2, 2)) {
1107 goto exit;
1108 }
1109 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
1110 goto exit;
1111 }
1112 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001113 _PyArg_BadArgument("ulaw2lin", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001114 goto exit;
1115 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001116 width = _PyLong_AsInt(args[1]);
1117 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001118 goto exit;
1119 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001120 return_value = audioop_ulaw2lin_impl(module, &fragment, width);
1121
1122exit:
1123 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001124 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001125 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001126 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001127
1128 return return_value;
1129}
1130
1131PyDoc_STRVAR(audioop_lin2alaw__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001132"lin2alaw($module, fragment, width, /)\n"
1133"--\n"
1134"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001135"Convert samples in the audio fragment to a-LAW encoding.");
1136
1137#define AUDIOOP_LIN2ALAW_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001138 {"lin2alaw", (PyCFunction)(void(*)(void))audioop_lin2alaw, METH_FASTCALL, audioop_lin2alaw__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001139
1140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001141audioop_lin2alaw_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001142
1143static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001144audioop_lin2alaw(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001145{
1146 PyObject *return_value = NULL;
1147 Py_buffer fragment = {NULL, NULL};
1148 int width;
1149
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001150 if (!_PyArg_CheckPositional("lin2alaw", nargs, 2, 2)) {
1151 goto exit;
1152 }
1153 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
1154 goto exit;
1155 }
1156 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001157 _PyArg_BadArgument("lin2alaw", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001158 goto exit;
1159 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001160 width = _PyLong_AsInt(args[1]);
1161 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001162 goto exit;
1163 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001164 return_value = audioop_lin2alaw_impl(module, &fragment, width);
1165
1166exit:
1167 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001168 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001169 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001170 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001171
1172 return return_value;
1173}
1174
1175PyDoc_STRVAR(audioop_alaw2lin__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001176"alaw2lin($module, fragment, width, /)\n"
1177"--\n"
1178"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001179"Convert sound fragments in a-LAW encoding to linearly encoded sound fragments.");
1180
1181#define AUDIOOP_ALAW2LIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001182 {"alaw2lin", (PyCFunction)(void(*)(void))audioop_alaw2lin, METH_FASTCALL, audioop_alaw2lin__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001183
1184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001185audioop_alaw2lin_impl(PyObject *module, Py_buffer *fragment, int width);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001186
1187static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001188audioop_alaw2lin(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001189{
1190 PyObject *return_value = NULL;
1191 Py_buffer fragment = {NULL, NULL};
1192 int width;
1193
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001194 if (!_PyArg_CheckPositional("alaw2lin", nargs, 2, 2)) {
1195 goto exit;
1196 }
1197 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
1198 goto exit;
1199 }
1200 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001201 _PyArg_BadArgument("alaw2lin", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001202 goto exit;
1203 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001204 width = _PyLong_AsInt(args[1]);
1205 if (width == -1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001206 goto exit;
1207 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001208 return_value = audioop_alaw2lin_impl(module, &fragment, width);
1209
1210exit:
1211 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001212 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001213 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001214 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001215
1216 return return_value;
1217}
1218
1219PyDoc_STRVAR(audioop_lin2adpcm__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001220"lin2adpcm($module, fragment, width, state, /)\n"
1221"--\n"
1222"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001223"Convert samples to 4 bit Intel/DVI ADPCM encoding.");
1224
1225#define AUDIOOP_LIN2ADPCM_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001226 {"lin2adpcm", (PyCFunction)(void(*)(void))audioop_lin2adpcm, METH_FASTCALL, audioop_lin2adpcm__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001227
1228static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001229audioop_lin2adpcm_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -04001230 PyObject *state);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001231
1232static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001233audioop_lin2adpcm(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001234{
1235 PyObject *return_value = NULL;
1236 Py_buffer fragment = {NULL, NULL};
1237 int width;
1238 PyObject *state;
1239
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001240 if (!_PyArg_CheckPositional("lin2adpcm", nargs, 3, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001241 goto exit;
1242 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001243 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
1244 goto exit;
1245 }
1246 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001247 _PyArg_BadArgument("lin2adpcm", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001248 goto exit;
1249 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001250 width = _PyLong_AsInt(args[1]);
1251 if (width == -1 && PyErr_Occurred()) {
1252 goto exit;
1253 }
1254 state = args[2];
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001255 return_value = audioop_lin2adpcm_impl(module, &fragment, width, state);
1256
1257exit:
1258 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001259 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001260 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001261 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001262
1263 return return_value;
1264}
1265
1266PyDoc_STRVAR(audioop_adpcm2lin__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08001267"adpcm2lin($module, fragment, width, state, /)\n"
1268"--\n"
1269"\n"
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001270"Decode an Intel/DVI ADPCM coded fragment to a linear fragment.");
1271
1272#define AUDIOOP_ADPCM2LIN_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001273 {"adpcm2lin", (PyCFunction)(void(*)(void))audioop_adpcm2lin, METH_FASTCALL, audioop_adpcm2lin__doc__},
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001274
1275static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001276audioop_adpcm2lin_impl(PyObject *module, Py_buffer *fragment, int width,
Larry Hastings89964c42015-04-14 18:07:59 -04001277 PyObject *state);
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001278
1279static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001280audioop_adpcm2lin(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001281{
1282 PyObject *return_value = NULL;
1283 Py_buffer fragment = {NULL, NULL};
1284 int width;
1285 PyObject *state;
1286
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001287 if (!_PyArg_CheckPositional("adpcm2lin", nargs, 3, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001288 goto exit;
1289 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001290 if (PyObject_GetBuffer(args[0], &fragment, PyBUF_SIMPLE) != 0) {
1291 goto exit;
1292 }
1293 if (!PyBuffer_IsContiguous(&fragment, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001294 _PyArg_BadArgument("adpcm2lin", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001295 goto exit;
1296 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001297 width = _PyLong_AsInt(args[1]);
1298 if (width == -1 && PyErr_Occurred()) {
1299 goto exit;
1300 }
1301 state = args[2];
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001302 return_value = audioop_adpcm2lin_impl(module, &fragment, width, state);
1303
1304exit:
1305 /* Cleanup for fragment */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001306 if (fragment.obj) {
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001307 PyBuffer_Release(&fragment);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001308 }
Serhiy Storchaka8d00d732014-01-25 11:57:59 +02001309
1310 return return_value;
1311}
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +03001312/*[clinic end generated code: output=840f8c315ebd4946 input=a9049054013a1b77]*/