aboutsummaryrefslogtreecommitdiffstats
path: root/tools/plot/pqf/pqf_binding.c
blob: 353e2de7b7ade2b5d016968f10bf54b5b288f28b (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <Python.h>
#include <structmember.h>

#include <libpqf.h>

typedef struct {
    PyObject_HEAD;
    pqf_a_ctx_t ctx;
} AnalyzeCtxObject;

static PyObject *
AnalyzeCtx_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    PyObject *seq;
    AnalyzeCtxObject *self;
    uint32_t i;
    float* proto;
    uint32_t proto_sz;
    uint32_t subband_sz;
    uint32_t subbands_num;

    self = (AnalyzeCtxObject*)type->tp_alloc(type, 0);

    if (!self) {
        return PyErr_NoMemory(  );
    }

    if (!PyArg_ParseTuple(args, "iiO",
                          &subband_sz, &subbands_num, &seq))
    {
        return NULL;
    }
    
    seq = PySequence_Fast(seq, "argument must be iterable");

    if (!seq) {
        return NULL;
    }

    proto_sz = PySequence_Fast_GET_SIZE(seq);

    proto = (float*)malloc(proto_sz * sizeof(float));

    if(!proto) {
        Py_DECREF(seq);
        return PyErr_NoMemory(  );
    }

    for (i = 0; i < proto_sz; i++) {
        PyObject *fitem;
        PyObject *item = PySequence_Fast_GET_ITEM(seq, i);
        if(!item) {
            Py_DECREF(seq);
            free(proto);
            return NULL;
        }

        fitem = PyNumber_Float(item);
        if(!fitem) {
            Py_DECREF(seq);
            free(proto);
            PyErr_SetString(PyExc_TypeError, "all items must be numbers");
            return NULL;
        }

        proto[i] = PyFloat_AS_DOUBLE(fitem);
        Py_DECREF(fitem);
    }

    pqf_status_t status = pqf_create_a_ctx(subband_sz, subbands_num, proto_sz,
                                           proto, &self->ctx);
 
    free(proto);

    if (status != PQF_SUCCESS) {
        switch (status) {
            case PQF_NOMEM:
                PyErr_SetString(PyExc_MemoryError, "No memory to create analyze context");
                break;
            case PQF_WRONG_SUBBAND_SZ:
                PyErr_SetString(PyExc_ValueError, "Unsupported subband size");
                break;
            case PQF_WRONG_SUBBANDS_NUM:
                PyErr_SetString(PyExc_ValueError, "Unsupported subbands number");
                break;
            case PQF_WRONG_PROTO_SZ:
                PyErr_SetString(PyExc_ValueError, "Unsupported prototype size");
                break;
            case PQF_FRAME_TOO_LONG:
                PyErr_SetString(PyExc_ValueError, "Frame too long");
                break;
            case PQF_CONTRACT_VIOLATION:
                PyErr_SetString(PyExc_ValueError, "Conract violation");
            break;
        }
        return NULL;
    }

    return (PyObject*)self;
}

static void
AnalyzeCtx_dealloc(AnalyzeCtxObject *self)
{
    pqf_free_a_ctx(self->ctx);
}

static PyObject* analyzectx_framesz(PyObject* self, PyObject* args)
{
    AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self;
    return PyLong_FromUnsignedLong(pqf_get_frame_sz(ctx->ctx));
}

static PyObject* analyzectx_subbandsz(PyObject* self, PyObject* args)
{
    AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self;
    return PyLong_FromUnsignedLong(pqf_get_subband_sz(ctx->ctx));
}

static PyObject* analyzectx_subbandsnum(PyObject* self, PyObject* args)
{
    AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self;
    return PyLong_FromUnsignedLong(pqf_get_subbands_num(ctx->ctx));
}

static PyObject* analyzectx_do(PyObject* self, PyObject* args)
{
    float* in;
    float* out;

    uint32_t i = 0;
    uint32_t len = 0;
    AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self;
    uint32_t frame_sz = pqf_get_frame_sz(ctx->ctx);

    PyObject *seq;

    if (!PyArg_ParseTuple(args, "O", &seq)) {
        return NULL;
    }

    seq = PySequence_Fast(seq, "argument must be iterable");

    if (!seq) {
        return NULL;
    }

    len = PySequence_Fast_GET_SIZE(seq);

    if (len != frame_sz) {
        PyErr_SetString(PyExc_ValueError, "wrong given frame size");
        return NULL;
    }

    in = (float*)malloc(len * sizeof(float));
    if(!in) {
        Py_DECREF(seq);
        return PyErr_NoMemory(  );
    }

    for (i = 0; i < len; i++) {
        PyObject *fitem;
        PyObject *item = PySequence_Fast_GET_ITEM(seq, i);
        if(!item) {
            Py_DECREF(seq);
            free(in);
            return NULL;
        }
        fitem = PyNumber_Float(item);
        if(!fitem) {
            Py_DECREF(seq);
            free(in);
            PyErr_SetString(PyExc_TypeError, "all items must be numbers");
            return NULL;
        }
        in[i] = PyFloat_AS_DOUBLE(fitem);
        Py_DECREF(fitem);
    }

    PyObject *my_list = PyList_New(0);
    if(my_list == NULL) {
        Py_DECREF(seq);
        free(in);
        return NULL;
    }

    out = (float*)calloc(pqf_get_frame_sz(ctx->ctx), sizeof(float));
    if (out == NULL) {
        Py_DECREF(seq);
        free(in);
        return NULL;
    }


    pqf_do_analyse(ctx->ctx, in, out);

    for (i = 0; i < len; i++) {
        PyObject *o = Py_BuildValue("f", out[i]);
        if(PyList_Append(my_list, o) == -1) {
            Py_DECREF(seq);
            free(in);
	    free(out);
            return NULL;
        }
    }

    free(in);
    free(out);
    return my_list;
}

static PyMethodDef methods[] = {
    { NULL, NULL, 0, NULL }
};

static PyMethodDef AnalyzeCtx_methods[] = {
    {"do", (PyCFunction)analyzectx_do, METH_VARARGS, NULL},
    {"frame_sz", (PyCFunction)analyzectx_framesz, METH_NOARGS, NULL},
    {"subband_sz", (PyCFunction)analyzectx_subbandsz, METH_NOARGS, NULL},
    {"subbands_num", (PyCFunction)analyzectx_subbandsnum, METH_NOARGS, NULL},
    {NULL}
};

static PyTypeObject AnalyzeCtxType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "pqf.AnalyzeCtx",
    .tp_doc = "AnalyzeCtx",
    .tp_basicsize = sizeof(AnalyzeCtxObject),
    .tp_itemsize = 0,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_new = AnalyzeCtx_new,
    .tp_dealloc = (destructor)AnalyzeCtx_dealloc,
    .tp_methods = AnalyzeCtx_methods,
};

static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT,
    .m_name = "pqf",
    .m_doc = "pqf library test binding",
    .m_size = -1,
    .m_methods = methods
};

PyMODINIT_FUNC PyInit_pqf(void)
{
    PyObject *m;
    if (PyType_Ready(&AnalyzeCtxType) < 0)
        return NULL;

    m = PyModule_Create(&module);
    if (m == NULL)
        return NULL;

    Py_INCREF(&AnalyzeCtxType);
    PyModule_AddObject(m, "AnalyzeCtx", (PyObject*)&AnalyzeCtxType);
    return m;
}