14930cef6SMatthias Ringwald /******************************************************************************
24930cef6SMatthias Ringwald *
34930cef6SMatthias Ringwald * Copyright 2022 Google LLC
44930cef6SMatthias Ringwald *
54930cef6SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License");
64930cef6SMatthias Ringwald * you may not use this file except in compliance with the License.
74930cef6SMatthias Ringwald * You may obtain a copy of the License at:
84930cef6SMatthias Ringwald *
94930cef6SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0
104930cef6SMatthias Ringwald *
114930cef6SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software
124930cef6SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS,
134930cef6SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
144930cef6SMatthias Ringwald * See the License for the specific language governing permissions and
154930cef6SMatthias Ringwald * limitations under the License.
164930cef6SMatthias Ringwald *
174930cef6SMatthias Ringwald ******************************************************************************/
184930cef6SMatthias Ringwald
194930cef6SMatthias Ringwald #include "lc3.h"
204930cef6SMatthias Ringwald
214930cef6SMatthias Ringwald #define PY_SSIZE_T_CLEAN
224930cef6SMatthias Ringwald #include <Python.h>
234930cef6SMatthias Ringwald #include <numpy/ndarrayobject.h>
244930cef6SMatthias Ringwald
254930cef6SMatthias Ringwald #include <energy.c>
264930cef6SMatthias Ringwald #include "ctypes.h"
274930cef6SMatthias Ringwald
energy_compute_py(PyObject * m,PyObject * args)284930cef6SMatthias Ringwald static PyObject *energy_compute_py(PyObject *m, PyObject *args)
294930cef6SMatthias Ringwald {
304930cef6SMatthias Ringwald unsigned dt, sr;
314930cef6SMatthias Ringwald PyObject *x_obj, *e_obj;
324930cef6SMatthias Ringwald float *x, *e;
334930cef6SMatthias Ringwald
344930cef6SMatthias Ringwald if (!PyArg_ParseTuple(args, "IIO", &dt, &sr, &x_obj))
354930cef6SMatthias Ringwald return NULL;
364930cef6SMatthias Ringwald
37*6897da5cSDirk Helbig CTYPES_CHECK("dt", dt < LC3_NUM_DT);
38*6897da5cSDirk Helbig CTYPES_CHECK("sr", sr < LC3_NUM_SRATE);
394930cef6SMatthias Ringwald
40*6897da5cSDirk Helbig int ns = lc3_ns(dt, sr);
414930cef6SMatthias Ringwald
424930cef6SMatthias Ringwald CTYPES_CHECK("x", to_1d_ptr(x_obj, NPY_FLOAT, ns, &x));
43*6897da5cSDirk Helbig e_obj = new_1d_ptr(NPY_FLOAT, lc3_num_bands[dt][sr], &e);
444930cef6SMatthias Ringwald
454930cef6SMatthias Ringwald int nn_flag = lc3_energy_compute(dt, sr, x, e);
464930cef6SMatthias Ringwald
474930cef6SMatthias Ringwald return Py_BuildValue("Ni", e_obj, nn_flag);
484930cef6SMatthias Ringwald }
494930cef6SMatthias Ringwald
504930cef6SMatthias Ringwald static PyMethodDef methods[] = {
514930cef6SMatthias Ringwald { "energy_compute", energy_compute_py, METH_VARARGS },
524930cef6SMatthias Ringwald { NULL },
534930cef6SMatthias Ringwald };
544930cef6SMatthias Ringwald
lc3_energy_py_init(PyObject * m)554930cef6SMatthias Ringwald PyMODINIT_FUNC lc3_energy_py_init(PyObject *m)
564930cef6SMatthias Ringwald {
574930cef6SMatthias Ringwald import_array();
584930cef6SMatthias Ringwald
594930cef6SMatthias Ringwald PyModule_AddFunctions(m, methods);
604930cef6SMatthias Ringwald
614930cef6SMatthias Ringwald return m;
624930cef6SMatthias Ringwald }
63