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 "lc3.h"
20
21 #define PY_SSIZE_T_CLEAN
22 #include <Python.h>
23 #include <numpy/ndarrayobject.h>
24
25 #include <energy.c>
26 #include "ctypes.h"
27
energy_compute_py(PyObject * m,PyObject * args)28 static PyObject *energy_compute_py(PyObject *m, PyObject *args)
29 {
30 unsigned dt, sr;
31 PyObject *x_obj, *e_obj;
32 float *x, *e;
33
34 if (!PyArg_ParseTuple(args, "IIO", &dt, &sr, &x_obj))
35 return NULL;
36
37 CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT);
38 CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE);
39
40 int ns = LC3_NS(dt, sr);
41
42 CTYPES_CHECK("x", to_1d_ptr(x_obj, NPY_FLOAT, ns, &x));
43 e_obj = new_1d_ptr(NPY_FLOAT, LC3_NUM_BANDS, &e);
44
45 int nn_flag = lc3_energy_compute(dt, sr, x, e);
46
47 return Py_BuildValue("Ni", e_obj, nn_flag);
48 }
49
50 static PyMethodDef methods[] = {
51 { "energy_compute", energy_compute_py, METH_VARARGS },
52 { NULL },
53 };
54
lc3_energy_py_init(PyObject * m)55 PyMODINIT_FUNC lc3_energy_py_init(PyObject *m)
56 {
57 import_array();
58
59 PyModule_AddFunctions(m, methods);
60
61 return m;
62 }
63