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 <ltpf.c> 23*4930cef6SMatthias Ringwald #include "ctypes.h" 24*4930cef6SMatthias Ringwald 25*4930cef6SMatthias Ringwald static PyObject *resample_py(PyObject *m, PyObject *args) 26*4930cef6SMatthias Ringwald { 27*4930cef6SMatthias Ringwald unsigned dt, sr; 28*4930cef6SMatthias Ringwald PyObject *hp50_obj, *x_obj, *y_obj; 29*4930cef6SMatthias Ringwald struct lc3_ltpf_hp50_state hp50; 30*4930cef6SMatthias Ringwald int16_t *x, *y; 31*4930cef6SMatthias Ringwald 32*4930cef6SMatthias Ringwald if (!PyArg_ParseTuple(args, "IIOOO", &dt, &sr, &hp50_obj, &x_obj, &y_obj)) 33*4930cef6SMatthias Ringwald return NULL; 34*4930cef6SMatthias Ringwald 35*4930cef6SMatthias Ringwald CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT); 36*4930cef6SMatthias Ringwald CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE); 37*4930cef6SMatthias Ringwald CTYPES_CHECK(NULL, hp50_obj = to_ltpf_hp50_state(hp50_obj, &hp50)); 38*4930cef6SMatthias Ringwald 39*4930cef6SMatthias Ringwald int ns = LC3_NS(dt, sr), nt = LC3_NT(dt); 40*4930cef6SMatthias Ringwald int ny = sizeof((struct lc3_ltpf_analysis){ }.x_12k8) / sizeof(int16_t); 41*4930cef6SMatthias Ringwald int n = dt == LC3_DT_7M5 ? 96 : 128; 42*4930cef6SMatthias Ringwald 43*4930cef6SMatthias Ringwald CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+nt, &x)); 44*4930cef6SMatthias Ringwald CTYPES_CHECK("y", y_obj = to_1d_ptr(y_obj, NPY_INT16, ny, &y)); 45*4930cef6SMatthias Ringwald 46*4930cef6SMatthias Ringwald resample_12k8[sr](&hp50, x + nt, y + (ny - n), n); 47*4930cef6SMatthias Ringwald 48*4930cef6SMatthias Ringwald from_ltpf_hp50_state(hp50_obj, &hp50); 49*4930cef6SMatthias Ringwald return Py_BuildValue("O", y_obj); 50*4930cef6SMatthias Ringwald } 51*4930cef6SMatthias Ringwald 52*4930cef6SMatthias Ringwald static PyObject *analyse_py(PyObject *m, PyObject *args) 53*4930cef6SMatthias Ringwald { 54*4930cef6SMatthias Ringwald PyObject *ltpf_obj, *x_obj; 55*4930cef6SMatthias Ringwald unsigned dt, sr; 56*4930cef6SMatthias Ringwald struct lc3_ltpf_analysis ltpf; 57*4930cef6SMatthias Ringwald struct lc3_ltpf_data data = { 0 }; 58*4930cef6SMatthias Ringwald int16_t *x; 59*4930cef6SMatthias Ringwald 60*4930cef6SMatthias Ringwald if (!PyArg_ParseTuple(args, "IIOO", &dt, &sr, <pf_obj, &x_obj)) 61*4930cef6SMatthias Ringwald return NULL; 62*4930cef6SMatthias Ringwald 63*4930cef6SMatthias Ringwald CTYPES_CHECK("dt", dt < LC3_NUM_DT); 64*4930cef6SMatthias Ringwald CTYPES_CHECK("sr", sr < LC3_NUM_SRATE); 65*4930cef6SMatthias Ringwald CTYPES_CHECK(NULL, ltpf_obj = to_ltpf_analysis(ltpf_obj, <pf)); 66*4930cef6SMatthias Ringwald 67*4930cef6SMatthias Ringwald int ns = LC3_NS(dt, sr), nt = LC3_NT(sr); 68*4930cef6SMatthias Ringwald 69*4930cef6SMatthias Ringwald CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+nt, &x)); 70*4930cef6SMatthias Ringwald 71*4930cef6SMatthias Ringwald int pitch_present = 72*4930cef6SMatthias Ringwald lc3_ltpf_analyse(dt, sr, <pf, x + nt, &data); 73*4930cef6SMatthias Ringwald 74*4930cef6SMatthias Ringwald from_ltpf_analysis(ltpf_obj, <pf); 75*4930cef6SMatthias Ringwald return Py_BuildValue("iN", pitch_present, new_ltpf_data(&data)); 76*4930cef6SMatthias Ringwald } 77*4930cef6SMatthias Ringwald 78*4930cef6SMatthias Ringwald static PyObject *synthesize_py(PyObject *m, PyObject *args) 79*4930cef6SMatthias Ringwald { 80*4930cef6SMatthias Ringwald PyObject *ltpf_obj, *data_obj, *x_obj; 81*4930cef6SMatthias Ringwald struct lc3_ltpf_synthesis ltpf; 82*4930cef6SMatthias Ringwald struct lc3_ltpf_data data; 83*4930cef6SMatthias Ringwald bool pitch_present; 84*4930cef6SMatthias Ringwald unsigned dt, sr; 85*4930cef6SMatthias Ringwald int nbytes; 86*4930cef6SMatthias Ringwald float *x; 87*4930cef6SMatthias Ringwald 88*4930cef6SMatthias Ringwald if (!PyArg_ParseTuple(args, "IIiOOO", 89*4930cef6SMatthias Ringwald &dt, &sr, &nbytes, <pf_obj, &data_obj, &x_obj)) 90*4930cef6SMatthias Ringwald return NULL; 91*4930cef6SMatthias Ringwald 92*4930cef6SMatthias Ringwald CTYPES_CHECK("dt", dt < LC3_NUM_DT); 93*4930cef6SMatthias Ringwald CTYPES_CHECK("sr", sr < LC3_NUM_SRATE); 94*4930cef6SMatthias Ringwald CTYPES_CHECK("nbytes", nbytes >= 20 && nbytes <= 400); 95*4930cef6SMatthias Ringwald CTYPES_CHECK(NULL, ltpf_obj = to_ltpf_synthesis(ltpf_obj, <pf)); 96*4930cef6SMatthias Ringwald 97*4930cef6SMatthias Ringwald if ((pitch_present = (data_obj != Py_None))) 98*4930cef6SMatthias Ringwald CTYPES_CHECK(NULL, data_obj = to_ltpf_data(data_obj, &data)); 99*4930cef6SMatthias Ringwald 100*4930cef6SMatthias Ringwald int ns = LC3_NS(dt,sr), nd = 18 * LC3_SRATE_KHZ(sr); 101*4930cef6SMatthias Ringwald 102*4930cef6SMatthias Ringwald CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_FLOAT, nd+ns, &x)); 103*4930cef6SMatthias Ringwald 104*4930cef6SMatthias Ringwald lc3_ltpf_synthesize(dt, sr, nbytes, 105*4930cef6SMatthias Ringwald <pf, pitch_present ? &data : NULL, x, x + nd); 106*4930cef6SMatthias Ringwald 107*4930cef6SMatthias Ringwald from_ltpf_synthesis(ltpf_obj, <pf); 108*4930cef6SMatthias Ringwald return Py_BuildValue("O", x_obj); 109*4930cef6SMatthias Ringwald } 110*4930cef6SMatthias Ringwald 111*4930cef6SMatthias Ringwald static PyObject *get_nbits_py(PyObject *m, PyObject *args) 112*4930cef6SMatthias Ringwald { 113*4930cef6SMatthias Ringwald int pitch_present; 114*4930cef6SMatthias Ringwald 115*4930cef6SMatthias Ringwald if (!PyArg_ParseTuple(args, "i", &pitch_present)) 116*4930cef6SMatthias Ringwald return NULL; 117*4930cef6SMatthias Ringwald 118*4930cef6SMatthias Ringwald int nbits = lc3_ltpf_get_nbits(pitch_present); 119*4930cef6SMatthias Ringwald 120*4930cef6SMatthias Ringwald return Py_BuildValue("i", nbits); 121*4930cef6SMatthias Ringwald } 122*4930cef6SMatthias Ringwald 123*4930cef6SMatthias Ringwald static PyMethodDef methods[] = { 124*4930cef6SMatthias Ringwald { "ltpf_resample" , resample_py , METH_VARARGS }, 125*4930cef6SMatthias Ringwald { "ltpf_analyse" , analyse_py , METH_VARARGS }, 126*4930cef6SMatthias Ringwald { "ltpf_synthesize", synthesize_py, METH_VARARGS }, 127*4930cef6SMatthias Ringwald { "ltpf_get_nbits" , get_nbits_py , METH_VARARGS }, 128*4930cef6SMatthias Ringwald { NULL }, 129*4930cef6SMatthias Ringwald }; 130*4930cef6SMatthias Ringwald 131*4930cef6SMatthias Ringwald PyMODINIT_FUNC lc3_ltpf_py_init(PyObject *m) 132*4930cef6SMatthias Ringwald { 133*4930cef6SMatthias Ringwald import_array(); 134*4930cef6SMatthias Ringwald 135*4930cef6SMatthias Ringwald PyModule_AddFunctions(m, methods); 136*4930cef6SMatthias Ringwald 137*4930cef6SMatthias Ringwald return m; 138*4930cef6SMatthias Ringwald } 139