xref: /btstack/3rd-party/lc3-google/test/ltpf_py.c (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
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 <Python.h>
204930cef6SMatthias Ringwald #include <numpy/ndarrayobject.h>
214930cef6SMatthias Ringwald 
224930cef6SMatthias Ringwald #include <ltpf.c>
234930cef6SMatthias Ringwald #include "ctypes.h"
244930cef6SMatthias Ringwald 
resample_py(PyObject * m,PyObject * args)254930cef6SMatthias Ringwald static PyObject *resample_py(PyObject *m, PyObject *args)
264930cef6SMatthias Ringwald {
274930cef6SMatthias Ringwald     unsigned dt, sr;
284930cef6SMatthias Ringwald     PyObject *hp50_obj, *x_obj, *y_obj;
294930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state hp50;
304930cef6SMatthias Ringwald     int16_t *x, *y;
314930cef6SMatthias Ringwald 
324930cef6SMatthias Ringwald     if (!PyArg_ParseTuple(args, "IIOOO", &dt, &sr, &hp50_obj, &x_obj, &y_obj))
334930cef6SMatthias Ringwald         return NULL;
344930cef6SMatthias Ringwald 
354930cef6SMatthias Ringwald     CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT);
364930cef6SMatthias Ringwald     CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE);
374930cef6SMatthias Ringwald     CTYPES_CHECK(NULL, hp50_obj = to_ltpf_hp50_state(hp50_obj, &hp50));
384930cef6SMatthias Ringwald 
39*6897da5cSDirk Helbig     int ns = lc3_ns(dt, sr), nt = lc3_nt(sr);
404930cef6SMatthias Ringwald     int ny = sizeof((struct lc3_ltpf_analysis){ }.x_12k8) / sizeof(int16_t);
41*6897da5cSDirk Helbig     int n  = (1 + dt) * 32;
424930cef6SMatthias Ringwald 
434930cef6SMatthias Ringwald     CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+nt, &x));
444930cef6SMatthias Ringwald     CTYPES_CHECK("y", y_obj = to_1d_ptr(y_obj, NPY_INT16, ny, &y));
454930cef6SMatthias Ringwald 
464930cef6SMatthias Ringwald     resample_12k8[sr](&hp50, x + nt, y + (ny - n), n);
474930cef6SMatthias Ringwald 
484930cef6SMatthias Ringwald     from_ltpf_hp50_state(hp50_obj, &hp50);
494930cef6SMatthias Ringwald     return Py_BuildValue("O", y_obj);
504930cef6SMatthias Ringwald }
514930cef6SMatthias Ringwald 
analyse_py(PyObject * m,PyObject * args)524930cef6SMatthias Ringwald static PyObject *analyse_py(PyObject *m, PyObject *args)
534930cef6SMatthias Ringwald {
544930cef6SMatthias Ringwald     PyObject *ltpf_obj, *x_obj;
554930cef6SMatthias Ringwald     unsigned dt, sr;
564930cef6SMatthias Ringwald     struct lc3_ltpf_analysis ltpf;
574930cef6SMatthias Ringwald     struct lc3_ltpf_data data = { 0 };
584930cef6SMatthias Ringwald     int16_t *x;
594930cef6SMatthias Ringwald 
604930cef6SMatthias Ringwald     if (!PyArg_ParseTuple(args, "IIOO", &dt, &sr, &ltpf_obj, &x_obj))
614930cef6SMatthias Ringwald         return NULL;
624930cef6SMatthias Ringwald 
634930cef6SMatthias Ringwald     CTYPES_CHECK("dt", dt < LC3_NUM_DT);
644930cef6SMatthias Ringwald     CTYPES_CHECK("sr", sr < LC3_NUM_SRATE);
654930cef6SMatthias Ringwald     CTYPES_CHECK(NULL, ltpf_obj = to_ltpf_analysis(ltpf_obj, &ltpf));
664930cef6SMatthias Ringwald 
67*6897da5cSDirk Helbig     int ns = lc3_ns(dt, sr), nt = lc3_nt(sr);
684930cef6SMatthias Ringwald 
694930cef6SMatthias Ringwald     CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+nt, &x));
704930cef6SMatthias Ringwald 
714930cef6SMatthias Ringwald     int pitch_present =
724930cef6SMatthias Ringwald         lc3_ltpf_analyse(dt, sr, &ltpf, x + nt, &data);
734930cef6SMatthias Ringwald 
744930cef6SMatthias Ringwald     from_ltpf_analysis(ltpf_obj, &ltpf);
754930cef6SMatthias Ringwald     return Py_BuildValue("iN", pitch_present, new_ltpf_data(&data));
764930cef6SMatthias Ringwald }
774930cef6SMatthias Ringwald 
synthesize_py(PyObject * m,PyObject * args)784930cef6SMatthias Ringwald static PyObject *synthesize_py(PyObject *m, PyObject *args)
794930cef6SMatthias Ringwald {
804930cef6SMatthias Ringwald     PyObject *ltpf_obj, *data_obj, *x_obj;
814930cef6SMatthias Ringwald     struct lc3_ltpf_synthesis ltpf;
824930cef6SMatthias Ringwald     struct lc3_ltpf_data data;
834930cef6SMatthias Ringwald     bool pitch_present;
844930cef6SMatthias Ringwald     unsigned dt, sr;
854930cef6SMatthias Ringwald     int nbytes;
864930cef6SMatthias Ringwald     float *x;
874930cef6SMatthias Ringwald 
884930cef6SMatthias Ringwald     if (!PyArg_ParseTuple(args, "IIiOOO",
894930cef6SMatthias Ringwald                 &dt, &sr, &nbytes, &ltpf_obj, &data_obj, &x_obj))
904930cef6SMatthias Ringwald         return NULL;
914930cef6SMatthias Ringwald 
924930cef6SMatthias Ringwald     CTYPES_CHECK("dt", dt < LC3_NUM_DT);
934930cef6SMatthias Ringwald     CTYPES_CHECK("sr", sr < LC3_NUM_SRATE);
944930cef6SMatthias Ringwald     CTYPES_CHECK("nbytes", nbytes >= 20 && nbytes <= 400);
954930cef6SMatthias Ringwald     CTYPES_CHECK(NULL, ltpf_obj = to_ltpf_synthesis(ltpf_obj, &ltpf));
964930cef6SMatthias Ringwald 
974930cef6SMatthias Ringwald     if ((pitch_present = (data_obj != Py_None)))
984930cef6SMatthias Ringwald         CTYPES_CHECK(NULL, data_obj = to_ltpf_data(data_obj, &data));
994930cef6SMatthias Ringwald 
100*6897da5cSDirk Helbig     int ns = lc3_ns(dt,sr), nd = 18 * (lc3_ns_4m[sr] / 4);
1014930cef6SMatthias Ringwald 
1024930cef6SMatthias Ringwald     CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_FLOAT, nd+ns, &x));
1034930cef6SMatthias Ringwald 
1044930cef6SMatthias Ringwald     lc3_ltpf_synthesize(dt, sr, nbytes,
1054930cef6SMatthias Ringwald         &ltpf, pitch_present ? &data : NULL, x, x + nd);
1064930cef6SMatthias Ringwald 
1074930cef6SMatthias Ringwald     from_ltpf_synthesis(ltpf_obj, &ltpf);
1084930cef6SMatthias Ringwald     return Py_BuildValue("O", x_obj);
1094930cef6SMatthias Ringwald }
1104930cef6SMatthias Ringwald 
get_nbits_py(PyObject * m,PyObject * args)1114930cef6SMatthias Ringwald static PyObject *get_nbits_py(PyObject *m, PyObject *args)
1124930cef6SMatthias Ringwald {
1134930cef6SMatthias Ringwald     int pitch_present;
1144930cef6SMatthias Ringwald 
1154930cef6SMatthias Ringwald     if (!PyArg_ParseTuple(args, "i", &pitch_present))
1164930cef6SMatthias Ringwald         return NULL;
1174930cef6SMatthias Ringwald 
1184930cef6SMatthias Ringwald     int nbits = lc3_ltpf_get_nbits(pitch_present);
1194930cef6SMatthias Ringwald 
1204930cef6SMatthias Ringwald     return Py_BuildValue("i", nbits);
1214930cef6SMatthias Ringwald }
1224930cef6SMatthias Ringwald 
1234930cef6SMatthias Ringwald static PyMethodDef methods[] = {
1244930cef6SMatthias Ringwald     { "ltpf_resample"  , resample_py  , METH_VARARGS },
1254930cef6SMatthias Ringwald     { "ltpf_analyse"   , analyse_py   , METH_VARARGS },
1264930cef6SMatthias Ringwald     { "ltpf_synthesize", synthesize_py, METH_VARARGS },
1274930cef6SMatthias Ringwald     { "ltpf_get_nbits" , get_nbits_py , METH_VARARGS },
1284930cef6SMatthias Ringwald     { NULL },
1294930cef6SMatthias Ringwald };
1304930cef6SMatthias Ringwald 
lc3_ltpf_py_init(PyObject * m)1314930cef6SMatthias Ringwald PyMODINIT_FUNC lc3_ltpf_py_init(PyObject *m)
1324930cef6SMatthias Ringwald {
1334930cef6SMatthias Ringwald     import_array();
1344930cef6SMatthias Ringwald 
1354930cef6SMatthias Ringwald     PyModule_AddFunctions(m, methods);
1364930cef6SMatthias Ringwald 
1374930cef6SMatthias Ringwald     return m;
1384930cef6SMatthias Ringwald }
139