summaryrefslogtreecommitdiff
path: root/system/embdrv/lc3/test/ltpf_py.c
blob: c51eadd6955d878269200f8af0217b3e8496eb34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/******************************************************************************
 *
 *  Copyright 2022 Google LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include <Python.h>
#include <numpy/ndarrayobject.h>

#include <ltpf.c>
#include "ctypes.h"

static PyObject *resample_py(PyObject *m, PyObject *args)
{
    unsigned dt, sr;
    PyObject *hp50_obj, *x_obj, *y_obj;
    struct lc3_ltpf_hp50_state hp50;
    int16_t *x, *y;

    if (!PyArg_ParseTuple(args, "IIOOO", &dt, &sr, &hp50_obj, &x_obj, &y_obj))
        return NULL;

    CTYPES_CHECK("dt", (unsigned)dt < LC3_NUM_DT);
    CTYPES_CHECK("sr", (unsigned)sr < LC3_NUM_SRATE);
    CTYPES_CHECK(NULL, hp50_obj = to_ltpf_hp50_state(hp50_obj, &hp50));

    int ns = LC3_NS(dt, sr), nt = LC3_NT(dt);
    int ny = sizeof((struct lc3_ltpf_analysis){ }.x_12k8) / sizeof(int16_t);
    int n  = dt == LC3_DT_7M5 ? 96 : 128;

    CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+nt, &x));
    CTYPES_CHECK("y", y_obj = to_1d_ptr(y_obj, NPY_INT16, ny, &y));

    resample_12k8[sr](&hp50, x + nt, y + (ny - n), n);

    from_ltpf_hp50_state(hp50_obj, &hp50);
    return Py_BuildValue("O", y_obj);
}

static PyObject *analyse_py(PyObject *m, PyObject *args)
{
    PyObject *ltpf_obj, *x_obj;
    unsigned dt, sr;
    struct lc3_ltpf_analysis ltpf;
    struct lc3_ltpf_data data = { 0 };
    int16_t *x;

    if (!PyArg_ParseTuple(args, "IIOO", &dt, &sr, &ltpf_obj, &x_obj))
        return NULL;

    CTYPES_CHECK("dt", dt < LC3_NUM_DT);
    CTYPES_CHECK("sr", sr < LC3_NUM_SRATE);
    CTYPES_CHECK(NULL, ltpf_obj = to_ltpf_analysis(ltpf_obj, &ltpf));

    int ns = LC3_NS(dt, sr), nt = LC3_NT(sr);

    CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_INT16, ns+nt, &x));

    int pitch_present =
        lc3_ltpf_analyse(dt, sr, &ltpf, x + nt, &data);

    from_ltpf_analysis(ltpf_obj, &ltpf);
    return Py_BuildValue("iN", pitch_present, new_ltpf_data(&data));
}

static PyObject *synthesize_py(PyObject *m, PyObject *args)
{
    PyObject *ltpf_obj, *data_obj, *x_obj;
    struct lc3_ltpf_synthesis ltpf;
    struct lc3_ltpf_data data;
    bool pitch_present;
    unsigned dt, sr;
    int nbytes;
    float *x;

    if (!PyArg_ParseTuple(args, "IIiOOO",
                &dt, &sr, &nbytes, &ltpf_obj, &data_obj, &x_obj))
        return NULL;

    CTYPES_CHECK("dt", dt < LC3_NUM_DT);
    CTYPES_CHECK("sr", sr < LC3_NUM_SRATE);
    CTYPES_CHECK("nbytes", nbytes >= 20 && nbytes <= 400);
    CTYPES_CHECK(NULL, ltpf_obj = to_ltpf_synthesis(ltpf_obj, &ltpf));

    if ((pitch_present = (data_obj != Py_None)))
        CTYPES_CHECK(NULL, data_obj = to_ltpf_data(data_obj, &data));

    int ns = LC3_NS(dt,sr), nd = 18 * LC3_SRATE_KHZ(sr);

    CTYPES_CHECK("x", x_obj = to_1d_ptr(x_obj, NPY_FLOAT, nd+ns, &x));

    lc3_ltpf_synthesize(dt, sr, nbytes,
        &ltpf, pitch_present ? &data : NULL, x, x + nd);

    from_ltpf_synthesis(ltpf_obj, &ltpf);
    return Py_BuildValue("O", x_obj);
}

static PyObject *get_nbits_py(PyObject *m, PyObject *args)
{
    int pitch_present;

    if (!PyArg_ParseTuple(args, "i", &pitch_present))
        return NULL;

    int nbits = lc3_ltpf_get_nbits(pitch_present);

    return Py_BuildValue("i", nbits);
}

static PyMethodDef methods[] = {
    { "ltpf_resample"  , resample_py  , METH_VARARGS },
    { "ltpf_analyse"   , analyse_py   , METH_VARARGS },
    { "ltpf_synthesize", synthesize_py, METH_VARARGS },
    { "ltpf_get_nbits" , get_nbits_py , METH_VARARGS },
    { NULL },
};

PyMODINIT_FUNC lc3_ltpf_py_init(PyObject *m)
{
    import_array();

    PyModule_AddFunctions(m, methods);

    return m;
}