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 26 static PyObject *mdct_forward_py(PyObject *m, PyObject *args) 27 { 28 unsigned dt, sr; 29 PyObject *x_obj, *xd_obj, *y_obj, *d_obj; 30 float *x, *xd, *y, *d; 31 32 if (!PyArg_ParseTuple(args, "IIOO", &dt, &sr, &x_obj, &xd_obj)) 33 return NULL; 34 35 CTYPES_CHECK("dt", dt < LC3_NUM_DT); 36 CTYPES_CHECK("sr", sr < LC3_NUM_SRATE); 37 38 int ns = lc3_ns(dt, sr), nd = lc3_nd(dt, sr); 39 40 CTYPES_CHECK("x", to_1d_ptr(x_obj, NPY_FLOAT, ns, &x)); 41 CTYPES_CHECK("xd", to_1d_ptr(xd_obj, NPY_FLOAT, nd, &xd)); 42 d_obj = new_1d_ptr(NPY_FLOAT, nd, &d); 43 y_obj = new_1d_ptr(NPY_FLOAT, ns, &y); 44 45 memcpy(d, xd, nd * sizeof(float)); 46 47 lc3_mdct_forward(dt, sr, sr, x, d, y); 48 49 return Py_BuildValue("NN", y_obj, d_obj); 50 } 51 52 static PyObject *mdct_inverse_py(PyObject *m, PyObject *args) 53 { 54 unsigned dt, sr; 55 PyObject *x_obj, *xd_obj, *d_obj, *y_obj; 56 float *x, *xd, *d, *y; 57 58 if (!PyArg_ParseTuple(args, "IIOO", &dt, &sr, &x_obj, &xd_obj)) 59 return NULL; 60 61 CTYPES_CHECK("dt", dt < LC3_NUM_DT); 62 CTYPES_CHECK("sr", sr < LC3_NUM_SRATE); 63 64 int ns = lc3_ns(dt, sr), nd = lc3_nd(dt, sr); 65 66 CTYPES_CHECK("x", to_1d_ptr(x_obj, NPY_FLOAT, ns, &x)); 67 CTYPES_CHECK("xd", to_1d_ptr(xd_obj, NPY_FLOAT, nd, &xd)); 68 d_obj = new_1d_ptr(NPY_FLOAT, nd, &d); 69 y_obj = new_1d_ptr(NPY_FLOAT, ns, &y); 70 71 memcpy(d, xd, nd * sizeof(float)); 72 73 lc3_mdct_inverse(dt, sr, sr, x, d, y); 74 75 return Py_BuildValue("NN", y_obj, d_obj); 76 } 77 78 static PyMethodDef methods[] = { 79 { "mdct_forward", mdct_forward_py, METH_VARARGS }, 80 { "mdct_inverse", mdct_inverse_py, METH_VARARGS }, 81 { NULL }, 82 }; 83 84 PyMODINIT_FUNC lc3_mdct_py_init(PyObject *m) 85 { 86 import_array(); 87 88 PyModule_AddFunctions(m, methods); 89 90 return m; 91 } 92