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 <attdet.c>
23 #include "ctypes.h"
24 
attdet_run_py(PyObject * m,PyObject * args)25 static PyObject *attdet_run_py(PyObject *m, PyObject *args)
26 {
27     unsigned dt, sr, nbytes;
28     PyObject *attdet_obj, *x_obj;
29     struct lc3_attdet_analysis attdet = { 0 };
30     int16_t *x;
31 
32     if (!PyArg_ParseTuple(args, "IIIOO",
33                 &dt, &sr, &nbytes, &attdet_obj, &x_obj))
34         return NULL;
35 
36     CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT);
37     CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE);
38     CTYPES_CHECK(NULL, attdet_obj = to_attdet_analysis(attdet_obj, &attdet));
39 
40     int ns = LC3_NS(dt, sr);
41 
42     CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+6, &x));
43 
44     int att = lc3_attdet_run(dt, sr, nbytes, &attdet, x+6);
45 
46     from_attdet_analysis(attdet_obj, &attdet);
47     return Py_BuildValue("i", att);
48 }
49 
50 static PyMethodDef methods[] = {
51     { "attdet_run", attdet_run_py, METH_VARARGS },
52     { NULL },
53 };
54 
lc3_attdet_py_init(PyObject * m)55 PyMODINIT_FUNC lc3_attdet_py_init(PyObject *m)
56 {
57     import_array();
58 
59     PyModule_AddFunctions(m, methods);
60 
61     return m;
62 }
63