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