1*4930cef6SMatthias Ringwald /****************************************************************************** 2*4930cef6SMatthias Ringwald * 3*4930cef6SMatthias Ringwald * Copyright 2022 Google LLC 4*4930cef6SMatthias Ringwald * 5*4930cef6SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License"); 6*4930cef6SMatthias Ringwald * you may not use this file except in compliance with the License. 7*4930cef6SMatthias Ringwald * You may obtain a copy of the License at: 8*4930cef6SMatthias Ringwald * 9*4930cef6SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0 10*4930cef6SMatthias Ringwald * 11*4930cef6SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software 12*4930cef6SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS, 13*4930cef6SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*4930cef6SMatthias Ringwald * See the License for the specific language governing permissions and 15*4930cef6SMatthias Ringwald * limitations under the License. 16*4930cef6SMatthias Ringwald * 17*4930cef6SMatthias Ringwald ******************************************************************************/ 18*4930cef6SMatthias Ringwald 19*4930cef6SMatthias Ringwald #include <Python.h> 20*4930cef6SMatthias Ringwald #include <numpy/ndarrayobject.h> 21*4930cef6SMatthias Ringwald 22*4930cef6SMatthias Ringwald #include <attdet.c> 23*4930cef6SMatthias Ringwald #include "ctypes.h" 24*4930cef6SMatthias Ringwald 25*4930cef6SMatthias Ringwald static PyObject *attdet_run_py(PyObject *m, PyObject *args) 26*4930cef6SMatthias Ringwald { 27*4930cef6SMatthias Ringwald unsigned dt, sr, nbytes; 28*4930cef6SMatthias Ringwald PyObject *attdet_obj, *x_obj; 29*4930cef6SMatthias Ringwald struct lc3_attdet_analysis attdet = { 0 }; 30*4930cef6SMatthias Ringwald int16_t *x; 31*4930cef6SMatthias Ringwald 32*4930cef6SMatthias Ringwald if (!PyArg_ParseTuple(args, "IIIOO", 33*4930cef6SMatthias Ringwald &dt, &sr, &nbytes, &attdet_obj, &x_obj)) 34*4930cef6SMatthias Ringwald return NULL; 35*4930cef6SMatthias Ringwald 36*4930cef6SMatthias Ringwald CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT); 37*4930cef6SMatthias Ringwald CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE); 38*4930cef6SMatthias Ringwald CTYPES_CHECK(NULL, attdet_obj = to_attdet_analysis(attdet_obj, &attdet)); 39*4930cef6SMatthias Ringwald 40*4930cef6SMatthias Ringwald int ns = LC3_NS(dt, sr); 41*4930cef6SMatthias Ringwald 42*4930cef6SMatthias Ringwald CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+6, &x)); 43*4930cef6SMatthias Ringwald 44*4930cef6SMatthias Ringwald int att = lc3_attdet_run(dt, sr, nbytes, &attdet, x+6); 45*4930cef6SMatthias Ringwald 46*4930cef6SMatthias Ringwald from_attdet_analysis(attdet_obj, &attdet); 47*4930cef6SMatthias Ringwald return Py_BuildValue("i", att); 48*4930cef6SMatthias Ringwald } 49*4930cef6SMatthias Ringwald 50*4930cef6SMatthias Ringwald static PyMethodDef methods[] = { 51*4930cef6SMatthias Ringwald { "attdet_run", attdet_run_py, METH_VARARGS }, 52*4930cef6SMatthias Ringwald { NULL }, 53*4930cef6SMatthias Ringwald }; 54*4930cef6SMatthias Ringwald 55*4930cef6SMatthias Ringwald PyMODINIT_FUNC lc3_attdet_py_init(PyObject *m) 56*4930cef6SMatthias Ringwald { 57*4930cef6SMatthias Ringwald import_array(); 58*4930cef6SMatthias Ringwald 59*4930cef6SMatthias Ringwald PyModule_AddFunctions(m, methods); 60*4930cef6SMatthias Ringwald 61*4930cef6SMatthias Ringwald return m; 62*4930cef6SMatthias Ringwald } 63