python/lib/gen_code.py: make sure aubio object has been created before deleting it
[aubio.git] / python / lib / gen_code.py
1 aubiodefvalue = {
2     # we have some clean up to do
3     'buf_size': 'Py_default_vector_length',
4     'win_s': 'Py_default_vector_length',
5     # and here too
6     'hop_size': 'Py_default_vector_length / 2',
7     'hop_s': 'Py_default_vector_length / 2',
8     # these should be alright
9     'samplerate': 'Py_aubio_default_samplerate',
10     # now for the non obvious ones
11     'n_filters': '40',
12     'n_coeffs': '13',
13     'nelems': '10',
14     'flow': '0.',
15     'fhig': '1.',
16     'ilow': '0.',
17     'ihig': '1.',
18     'thrs': '0.5',
19     'ratio': '0.5',
20     'method': '"default"',
21     'uri': '"none"',
22     }
23
24 member_types = {
25         'name': 'type',
26         'char_t*': 'T_STRING',
27         'uint_t': 'T_INT',
28         'smpl_t': 'AUBIO_NPY_SMPL',
29         }
30
31 pyfromtype_fn = {
32         'smpl_t': 'PyFloat_FromDouble',
33         'uint_t': 'PyLong_FromLong', # was: 'PyInt_FromLong',
34         'fvec_t*': 'PyAubio_CFvecToArray',
35         'fmat_t*': 'PyAubio_CFmatToArray',
36         }
37
38 pytoaubio_fn = {
39         'fvec_t*': 'PyAubio_ArrayToCFvec',
40         'cvec_t*': 'PyAubio_PyCvecToCCvec',
41         #'fmat_t*': 'PyAubio_ArrayToCFmat',
42         }
43
44 pyfromaubio_fn = {
45         'fvec_t*': 'PyAubio_CFvecToArray',
46         'cvec_t*': 'PyAubio_CCvecToArray',
47         'fmat_t*': 'PyAubio_CFmatToArray',
48         }
49
50 newfromtype_fn = {
51         'fvec_t*': 'new_py_fvec',
52         'fmat_t*': 'new_py_fmat',
53         'cvec_t*': 'new_py_cvec',
54         }
55
56 delfromtype_fn = {
57         'fvec_t*': 'Py_DECREF',
58         'fmat_t*': 'Py_DECREF',
59         'cvec_t*': 'Py_DECREF',
60         }
61
62 param_init = {
63         'char_t*': 'NULL',
64         'uint_t': '0',
65         'sint_t': 0,
66         'smpl_t': 0.,
67         'lsmp_t': 0.,
68         }
69
70 pyargparse_chars = {
71         'smpl_t': 'f', # if not usedouble else 'd',
72         'uint_t': 'I',
73         'sint_t': 'I',
74         'char_t*': 's',
75         'fmat_t*': 'O',
76         'fvec_t*': 'O',
77         'cvec_t*': 'O',
78         }
79
80 objoutsize = {
81         'onset': '1',
82         'pitch': '1',
83         'wavetable': 'self->hop_size',
84         'sampler': 'self->hop_size',
85         'mfcc': 'self->n_coeffs',
86         'specdesc': '1',
87         'tempo': '1',
88         'filterbank': 'self->n_filters',
89         'tss': 'self->hop_size',
90         }
91
92 def get_name(proto):
93     name = proto.replace(' *', '* ').split()[1].split('(')[0]
94     name = name.replace('*','')
95     if name == '': raise ValueError(proto + "gave empty name")
96     return name
97
98 def get_return_type(proto):
99     import re
100     paramregex = re.compile('(\w+ ?\*?).*')
101     outputs = paramregex.findall(proto)
102     assert len(outputs) == 1
103     return outputs[0].replace(' ', '')
104
105 def split_type(arg):
106     """ arg = 'foo *name' 
107         return ['foo*', 'name'] """
108     l = arg.split()
109     type_arg = {} #'type': l[0], 'name': l[1]}
110     type_arg['type'] = " ".join(l[:-1])
111     type_arg['name'] = l[-1]
112     # fix up type / name
113     if type_arg['name'].startswith('*'):
114         # ['foo', '*name'] -> ['foo*', 'name']
115         type_arg['type'] += '*'
116         type_arg['name'] = type_arg['name'][1:]
117     if type_arg['type'].endswith(' *'):
118         # ['foo *', 'name'] -> ['foo*', 'name']
119         type_arg['type'] = type_arg['type'].replace(' *','*')
120     if type_arg['type'].startswith('const '):
121         # ['foo *', 'name'] -> ['foo*', 'name']
122         type_arg['type'] = type_arg['type'].replace('const ','')
123     return type_arg
124
125 def get_params(proto):
126     """ get the list of parameters from a function prototype
127     example: proto = "int main (int argc, char ** argv)"
128     returns: ['int argc', 'char ** argv']
129     """
130     import re
131     paramregex = re.compile('.*\((.*)\);')
132     a = paramregex.findall(proto)[0].split(', ')
133     #a = [i.replace('const ', '') for i in a]
134     return a
135
136 def get_input_params(proto):
137     a = get_params(proto)
138     return [i.replace('const ', '') for i in a if (i.startswith('const ') or i.startswith('uint_t ') or i.startswith('smpl_t '))]
139
140 def get_output_params(proto):
141     a = get_params(proto)
142     return [i for i in a if not i.startswith('const ')][1:]
143
144 def get_params_types_names(proto):
145     """ get the list of parameters from a function prototype
146     example: proto = "int main (int argc, char ** argv)"
147     returns: [['int', 'argc'], ['char **','argv']]
148     """
149     a = list(map(split_type, get_params(proto)))
150     #print proto, a
151     #import sys; sys.exit(1)
152     return a
153
154 class MappedObject(object):
155
156     def __init__(self, prototypes, usedouble = False):
157         if usedouble:
158             pyargparse_chars['smpl_t'] = 'd'
159         self.prototypes = prototypes
160
161         self.shortname = prototypes['shortname']
162         self.longname = prototypes['longname']
163         self.new_proto = prototypes['new'][0]
164         self.del_proto = prototypes['del'][0]
165         self.do_proto = prototypes['do'][0]
166         self.input_params = get_params_types_names(self.new_proto)
167         self.input_params_list = "; ".join(get_input_params(self.new_proto))
168         self.outputs = get_params_types_names(self.do_proto)[2:]
169         self.do_inputs = [get_params_types_names(self.do_proto)[1]]
170         self.do_outputs = get_params_types_names(self.do_proto)[2:]
171         struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs]
172         self.struct_outputs = ";\n    ".join(struct_output_str)
173
174         #print ("input_params: ", map(split_type, get_input_params(self.do_proto)))
175         #print ("output_params", map(split_type, get_output_params(self.do_proto)))
176
177     def gen_code(self):
178         out = ""
179         out += self.gen_struct()
180         out += self.gen_doc()
181         out += self.gen_new()
182         out += self.gen_init()
183         out += self.gen_del()
184         out += self.gen_do()
185         out += self.gen_memberdef()
186         out += self.gen_set()
187         out += self.gen_get()
188         out += self.gen_methodef()
189         out += self.gen_typeobject()
190         return out
191
192     def gen_struct(self):
193         out = """
194 // {shortname} structure
195 typedef struct{{
196     PyObject_HEAD
197     // pointer to aubio object
198     {longname} *o;
199     // input parameters
200     {input_params_list};
201     // do input vectors
202     {do_inputs_list};
203     // output results
204     {struct_outputs};
205 }} Py_{shortname};
206 """
207         # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname}
208         do_inputs_list = "; ".join(get_input_params(self.do_proto)).replace('fvec_t *','fvec_t').replace('fmat_t *', 'fmat_t').replace('cvec_t *', 'cvec_t')
209         return out.format(do_inputs_list = do_inputs_list, **self.__dict__)
210
211     def gen_doc(self):
212         out = """
213 // TODO: add documentation
214 static char Py_{shortname}_doc[] = \"undefined\";
215 """
216         return out.format(**self.__dict__)
217
218     def gen_new(self):
219         out = """
220 // new {shortname}
221 static PyObject *
222 Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
223 {{
224     Py_{shortname} *self;
225 """.format(**self.__dict__)
226         params = self.input_params
227         for p in params:
228             out += """
229     {type} {name} = {defval};""".format(defval = param_init[p['type']], **p)
230         plist = ", ".join(["\"%s\"" % p['name'] for p in params])
231         out += """
232     static char *kwlist[] = {{ {plist}, NULL }};""".format(plist = plist)
233         argchars = "".join([pyargparse_chars[p['type']] for p in params])
234         arglist = ", ".join(["&%s" % p['name'] for p in params])
235         out += """
236     if (!PyArg_ParseTupleAndKeywords (args, kwds, "|{argchars}", kwlist,
237               {arglist})) {{
238         return NULL;
239     }}
240 """.format(argchars = argchars, arglist = arglist)
241         out += """
242     self = (Py_{shortname} *) pytype->tp_alloc (pytype, 0);
243     if (self == NULL) {{
244         return NULL;
245     }}
246 """.format(**self.__dict__)
247         params = self.input_params
248         for p in params:
249             out += self.check_valid(p)
250         out += """
251     return (PyObject *)self;
252 }
253 """
254         return out
255
256     def check_valid(self, p):
257         if p['type'] == 'uint_t':
258             return self.check_valid_uint(p)
259         if p['type'] == 'char_t*':
260             return self.check_valid_char(p)
261         else:
262             print ("ERROR, no idea how to check %s for validity" % p['type'])
263
264     def check_valid_uint(self, p):
265         name = p['name']
266         return """
267     self->{name} = {defval};
268     if ((sint_t){name} > 0) {{
269         self->{name} = {name};
270     }} else if ((sint_t){name} < 0) {{
271         PyErr_SetString (PyExc_ValueError, "can not use negative value for {name}");
272         return NULL;
273     }}
274 """.format(defval = aubiodefvalue[name], name = name)
275
276     def check_valid_char(self, p):
277         name = p['name']
278         return """
279     self->{name} = {defval};
280     if ({name} != NULL) {{
281         self->{name} = {name};
282     }}
283 """.format(defval = aubiodefvalue[name], name = name)
284
285     def gen_init(self):
286         out = """
287 // init {shortname}
288 static int
289 Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds)
290 {{
291 """.format(**self.__dict__)
292         new_name = get_name(self.new_proto)
293         new_params = ", ".join(["self->%s" % s['name'] for s in self.input_params])
294         out += """
295   self->o = {new_name}({new_params});
296 """.format(new_name = new_name, new_params = new_params)
297         paramchars = "%s"
298         paramvals = "self->method"
299         out += """
300   // return -1 and set error string on failure
301   if (self->o == NULL) {{
302     //char_t errstr[30 + strlen(self->uri)];
303     //sprintf(errstr, "error creating {shortname} with params {paramchars}", {paramvals});
304     char_t errstr[60];
305     sprintf(errstr, "error creating {shortname} with given params");
306     PyErr_SetString (PyExc_Exception, errstr);
307     return -1;
308   }}
309 """.format(paramchars = paramchars, paramvals = paramvals, **self.__dict__)
310         output_create = ""
311         for o in self.outputs:
312             output_create += """
313   self->{name} = {create_fn}({output_size});""".format(name = o['name'], create_fn = newfromtype_fn[o['type']], output_size = objoutsize[self.shortname])
314         out += """
315   // TODO get internal params after actual object creation?
316 """
317         out += """
318   // create outputs{output_create}
319 """.format(output_create = output_create)
320         out += """
321   return 0;
322 }
323 """
324         return out
325
326     def gen_memberdef(self):
327         out = """
328 static PyMemberDef Py_{shortname}_members[] = {{
329 """.format(**self.__dict__)
330         for p in get_params_types_names(self.new_proto):
331             tmp = "  {{\"{name}\", {ttype}, offsetof (Py_{shortname}, {name}), READONLY, \"TODO documentation\"}},\n"
332             pytype = member_types[p['type']]
333             out += tmp.format(name = p['name'], ttype = pytype, shortname = self.shortname)
334         out += """  {NULL}, // sentinel
335 };
336 """
337         return out
338
339     def gen_del(self):
340         out = """
341 // del {shortname}
342 static void
343 Py_{shortname}_del  (Py_{shortname} * self, PyObject * unused)
344 {{""".format(**self.__dict__)
345         for input_param in self.do_inputs:
346             if input_param['type'] == 'fmat_t *':
347                 out += """
348     free(self->{0[name]}.data);""".format(input_param)
349         for o in self.outputs:
350             name = o['name']
351             del_out = delfromtype_fn[o['type']]
352             out += """
353     {del_out}(self->{name});""".format(del_out = del_out, name = name)
354         del_fn = get_name(self.del_proto)
355         out += """
356     if (self->o) {{
357         {del_fn}(self->o);
358     }}
359     Py_TYPE(self)->tp_free((PyObject *) self);
360 }}
361 """.format(del_fn = del_fn)
362         return out
363
364     def gen_do(self):
365         output = self.outputs[0]
366         out = """
367 // do {shortname}
368 static PyObject*
369 Py_{shortname}_do  (Py_{shortname} * self, PyObject * args)
370 {{""".format(**self.__dict__)
371         input_params = self.do_inputs
372         output_params = self.do_outputs
373         #print input_params
374         #print output_params
375         for input_param in input_params:
376             out += """
377     PyObject *py_{0};""".format(input_param['name'], input_param['type'])
378         refs = ", ".join(["&py_%s" % p['name'] for p in input_params])
379         pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params])
380         out += """
381     if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
382         return NULL;
383     }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
384         for input_param in input_params:
385             out += """
386
387     if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{
388         return NULL;
389     }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
390         out += """
391
392     // TODO: check input sizes"""
393         for output_param in output_params:
394             out += """
395
396     Py_INCREF(self->{0[name]});
397     if (!{pytoaubio}(self->{0[name]}, &(self->c_{0[name]}))) {{
398         return NULL;
399     }}""".format(output_param, pytoaubio = pytoaubio_fn[output_param['type']])
400         do_fn = get_name(self.do_proto)
401         inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params])
402         c_outputs = ", ".join(["&(self->c_%s)" % p['name'] for p in self.do_outputs])
403         outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
404         out += """
405
406     {do_fn}(self->o, {inputs}, {c_outputs});
407
408     return {outputs};
409 }}
410 """.format(
411         do_fn = do_fn,
412         inputs = inputs, outputs = outputs, c_outputs = c_outputs,
413         )
414         return out
415
416     def gen_set(self):
417         out = """
418 // {shortname} setters
419 """.format(**self.__dict__)
420         for set_param in self.prototypes['set']:
421             params = get_params_types_names(set_param)[1]
422             paramtype = params['type']
423             method_name = get_name(set_param)
424             param = method_name.split('aubio_'+self.shortname+'_set_')[-1]
425             pyparamtype = pyargparse_chars[paramtype]
426             out += """
427 static PyObject *
428 Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args)
429 {{
430   uint_t err = 0;
431   {paramtype} {param};
432
433   if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{
434     return NULL;
435   }}
436   err = aubio_{shortname}_set_{param} (self->o, {param});
437
438   if (err > 0) {{
439     PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}");
440     return NULL;
441   }}
442   Py_RETURN_NONE;
443 }}
444 """.format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__)
445         return out
446
447     def gen_get(self):
448         out = """
449 // {shortname} getters
450 """.format(**self.__dict__)
451         for method in self.prototypes['get']:
452             params = get_params_types_names(method)
453             method_name = get_name(method)
454             assert len(params) == 1, \
455                 "get method has more than one parameter %s" % params
456             param = method_name.split('aubio_'+self.shortname+'_get_')[-1]
457             paramtype = get_return_type(method)
458             ptypeconv = pyfromtype_fn[paramtype]
459             out += """
460 static PyObject *
461 Pyaubio_{shortname}_get_{param} (Py_{shortname} *self, PyObject *unused)
462 {{
463   {ptype} {param} = aubio_{shortname}_get_{param} (self->o);
464   return (PyObject *){ptypeconv} ({param});
465 }}
466 """.format(param = param, ptype = paramtype, ptypeconv = ptypeconv,
467         **self.__dict__)
468         return out
469
470     def gen_methodef(self):
471         out = """
472 static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__)
473         for m in self.prototypes['set']:
474             name = get_name(m)
475             shortname = name.replace('aubio_%s_' % self.shortname, '')
476             out += """
477   {{"{shortname}", (PyCFunction) Py{name},
478     METH_VARARGS, ""}},""".format(name = name, shortname = shortname)
479         for m in self.prototypes['get']:
480             name = get_name(m)
481             shortname = name.replace('aubio_%s_' % self.shortname, '')
482             out += """
483   {{"{shortname}", (PyCFunction) Py{name},
484     METH_NOARGS, ""}},""".format(name = name, shortname = shortname)
485         out += """
486   {NULL} /* sentinel */
487 };
488 """
489         return out
490
491     def gen_typeobject(self):
492         return """
493 PyTypeObject Py_{shortname}Type = {{
494   //PyObject_HEAD_INIT (NULL)
495   //0,
496   PyVarObject_HEAD_INIT (NULL, 0)
497   "aubio.{shortname}",
498   sizeof (Py_{shortname}),
499   0,
500   (destructor) Py_{shortname}_del,
501   0,
502   0,
503   0,
504   0,
505   0,
506   0,
507   0,
508   0,
509   0,
510   (ternaryfunc)Py_{shortname}_do,
511   0,
512   0,
513   0,
514   0,
515   Py_TPFLAGS_DEFAULT,
516   Py_{shortname}_doc,
517   0,
518   0,
519   0,
520   0,
521   0,
522   0,
523   Py_{shortname}_methods,
524   Py_{shortname}_members,
525   0,
526   0,
527   0,
528   0,
529   0,
530   0,
531   (initproc) Py_{shortname}_init,
532   0,
533   Py_{shortname}_new,
534 }};
535 """.format(**self.__dict__)