aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac3plus_pqf/tools/plot/pqf/pqf_binding.c
blob: 8ddf5ee8025d243031ec7cee80fcc9168a4121aa (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
#include <Python.h>
#include <structmember.h>

#include <stdio.h>


#include <atrac3plus_pqf.h>

typedef struct {
    PyObject_HEAD;
    at3plus_pqf_a_ctx_t ctx;
} AnalyzeCtxObject;

static PyObject *
AnalyzeCtx_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    AnalyzeCtxObject *self;
    self = (AnalyzeCtxObject*)type->tp_alloc(type, 0);
    if (self == NULL) {
        fprintf(stderr, "Unable to allocate analyze context\n");
        return NULL;
    }
    return (PyObject*)self;
}

static int
AnalyzeCtx_init(AnalyzeCtxObject *self, PyObject *args, PyObject *kwds)
{
    self->ctx = at3plus_pqf_create_a_ctx();
    return 0;
}

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

static PyObject* prototype(PyObject* self, PyObject* unused)
{
    int i;
    const float* proto = at3plus_pqf_get_proto();

    PyObject *my_list = PyList_New(0);
    if(my_list == NULL) {
        return NULL;
    }

    for (i = 0; i < at3plus_pqf_proto_sz; i++) {
        PyObject *o = Py_BuildValue("f", proto[i]);
        if(PyList_Append(my_list, o) == -1) {
            return NULL;
        }
    }

    return my_list;
}

static PyObject* analyzectx_do(PyObject* self, PyObject* args)
{
    int i = 0;
    int len = 0;
    float* in;
    float* out;

    AnalyzeCtxObject* 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 != at3plus_pqf_frame_sz) {
        fprintf(stderr, "wrong frame size, expected: %d, got: %d", (int)at3plus_pqf_frame_sz, len);
	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(at3plus_pqf_frame_sz, sizeof(float));
    if (out == NULL) {
        Py_DECREF(seq);
        free(in);
        return NULL;
    }

    ctx = (AnalyzeCtxObject*)self;

    at3plus_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);
            return NULL;
        }
    }

    free(in);
    return my_list;
}

static PyMethodDef methods[] = {
    { "prototype", prototype, METH_NOARGS, "Show atrac3plus pqf filter prototype"},
    { NULL, NULL, 0, NULL }
};

static PyMethodDef AnalyzeCtx_methods[] = {
    {"do", (PyCFunction)analyzectx_do, METH_VARARGS, 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_init = (initproc)AnalyzeCtx_init,
    .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);
    PyModule_AddIntConstant(m, "frame_sz", at3plus_pqf_frame_sz); 
    return m;
}