1 /******************************************************************************
2 *
3 * Copyright 2022 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <Python.h>
20 #include <numpy/ndarrayobject.h>
21
22 #include <mdct.c>
23 #include "ctypes.h"
24
25
mdct_forward_py(PyObject * m,PyObject * args)26 static PyObject *mdct_forward_py(PyObject *m, PyObject *args)
27 {
28 PyObject *x_obj, *xd_obj, *y_obj, *d_obj;
29 enum lc3_dt dt;
30 enum lc3_srate sr;
31 float *x, *xd, *y, *d;
32
33 if (!PyArg_ParseTuple(args, "iiOO", &dt, &sr, &x_obj, &xd_obj))
34 return NULL;
35
36 CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT);
37 CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE);
38
39 int ns = LC3_NS(dt, sr), nd = LC3_ND(dt, sr);
40
41 CTYPES_CHECK("x", to_1d_ptr(x_obj, NPY_FLOAT, ns, &x));
42 CTYPES_CHECK("xd", to_1d_ptr(xd_obj, NPY_FLOAT, nd, &xd));
43 d_obj = new_1d_ptr(NPY_FLOAT, nd, &d);
44 y_obj = new_1d_ptr(NPY_FLOAT, ns, &y);
45
46 memcpy(d, xd, nd * sizeof(float));
47
48 lc3_mdct_forward(dt, sr, sr, x, d, y);
49
50 return Py_BuildValue("NN", y_obj, d_obj);
51 }
52
mdct_inverse_py(PyObject * m,PyObject * args)53 static PyObject *mdct_inverse_py(PyObject *m, PyObject *args)
54 {
55 PyObject *x_obj, *xd_obj, *d_obj, *y_obj;
56 enum lc3_dt dt;
57 enum lc3_srate sr;
58 float *x, *xd, *d, *y;
59
60 if (!PyArg_ParseTuple(args, "iiOO", &dt, &sr, &x_obj, &xd_obj))
61 return NULL;
62
63 CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT);
64 CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE);
65
66 int ns = LC3_NS(dt, sr), nd = LC3_ND(dt, sr);
67
68 CTYPES_CHECK("x", to_1d_ptr(x_obj, NPY_FLOAT, ns, &x));
69 CTYPES_CHECK("xd", to_1d_ptr(xd_obj, NPY_FLOAT, nd, &xd));
70 d_obj = new_1d_ptr(NPY_FLOAT, nd, &d);
71 y_obj = new_1d_ptr(NPY_FLOAT, ns, &y);
72
73 memcpy(d, xd, nd * sizeof(float));
74
75 lc3_mdct_inverse(dt, sr, sr, x, d, y);
76
77 return Py_BuildValue("NN", y_obj, d_obj);
78 }
79
80 static PyMethodDef methods[] = {
81 { "mdct_forward", mdct_forward_py, METH_VARARGS },
82 { "mdct_inverse", mdct_inverse_py, METH_VARARGS },
83 { NULL },
84 };
85
lc3_mdct_py_init(PyObject * m)86 PyMODINIT_FUNC lc3_mdct_py_init(PyObject *m)
87 {
88 import_array();
89
90 PyModule_AddFunctions(m, methods);
91
92 return m;
93 }
94