2 # we have some clean up to do
3 'buf_size': 'Py_default_vector_length',
4 'win_s': 'Py_default_vector_length',
5 'size': 'Py_default_vector_length',
7 'hop_size': 'Py_default_vector_length / 2',
8 'hop_s': 'Py_default_vector_length / 2',
9 # these should be alright
10 'samplerate': 'Py_aubio_default_samplerate',
11 # now for the non obvious ones
21 'method': '"default"',
28 'char_t*': 'T_STRING',
30 'smpl_t': 'AUBIO_NPY_SMPL',
34 'smpl_t': 'PyFloat_FromDouble',
35 'uint_t': 'PyLong_FromLong', # was: 'PyInt_FromLong',
36 'fvec_t*': 'PyAubio_CFvecToArray',
37 'fmat_t*': 'PyAubio_CFmatToArray',
41 'fvec_t*': 'PyAubio_ArrayToCFvec',
42 'cvec_t*': 'PyAubio_PyCvecToCCvec',
43 #'fmat_t*': 'PyAubio_ArrayToCFmat',
47 'fvec_t*': 'new_py_fvec',
48 'fmat_t*': 'new_py_fmat',
49 'cvec_t*': 'new_py_cvec',
53 'fvec_t*': 'Py_DECREF',
54 'fmat_t*': 'Py_DECREF',
55 'cvec_t*': 'Py_DECREF',
67 'smpl_t': 'f', # if not usedouble else 'd',
80 'wavetable': 'self->hop_size',
81 'sampler': 'self->hop_size',
82 'mfcc': 'self->n_coeffs',
85 'filterbank': 'self->n_filters',
86 'tss': 'self->buf_size',
87 'pitchshift': 'self->hop_size',
92 'mfcc': 'self->buf_size / 2 + 1',
93 'notes': 'self->hop_size',
94 'onset': 'self->hop_size',
95 'pitch': 'self->hop_size',
96 'sampler': 'self->hop_size',
97 'specdesc': 'self->buf_size / 2 + 1',
98 'tempo': 'self->hop_size',
99 'wavetable': 'self->hop_size',
100 'tss': 'self->buf_size / 2 + 1',
101 'pitchshift': 'self->hop_size',
105 name = proto.replace(' *', '* ').split()[1].split('(')[0]
106 name = name.replace('*','')
107 if name == '': raise ValueError(proto + "gave empty name")
110 def get_return_type(proto):
112 paramregex = re.compile('(\w+ ?\*?).*')
113 outputs = paramregex.findall(proto)
114 assert len(outputs) == 1
115 return outputs[0].replace(' ', '')
118 """ arg = 'foo *name'
119 return ['foo*', 'name'] """
121 type_arg = {} #'type': l[0], 'name': l[1]}
122 type_arg['type'] = " ".join(l[:-1])
123 type_arg['name'] = l[-1]
125 if type_arg['name'].startswith('*'):
126 # ['foo', '*name'] -> ['foo*', 'name']
127 type_arg['type'] += '*'
128 type_arg['name'] = type_arg['name'][1:]
129 if type_arg['type'].endswith(' *'):
130 # ['foo *', 'name'] -> ['foo*', 'name']
131 type_arg['type'] = type_arg['type'].replace(' *','*')
132 if type_arg['type'].startswith('const '):
133 # ['foo *', 'name'] -> ['foo*', 'name']
134 type_arg['type'] = type_arg['type'].replace('const ','')
137 def get_params(proto):
138 """ get the list of parameters from a function prototype
139 example: proto = "int main (int argc, char ** argv)"
140 returns: ['int argc', 'char ** argv']
143 paramregex = re.compile('.*\((.*)\);')
144 a = paramregex.findall(proto)[0].split(', ')
145 #a = [i.replace('const ', '') for i in a]
148 def get_input_params(proto):
149 a = get_params(proto)
150 return [i.replace('const ', '') for i in a if (i.startswith('const ') or i.startswith('uint_t ') or i.startswith('smpl_t '))]
152 def get_output_params(proto):
153 a = get_params(proto)
154 return [i for i in a if not i.startswith('const ')][1:]
156 def get_params_types_names(proto):
157 """ get the list of parameters from a function prototype
158 example: proto = "int main (int argc, char ** argv)"
159 returns: [['int', 'argc'], ['char **','argv']]
161 a = list(map(split_type, get_params(proto)))
163 #import sys; sys.exit(1)
166 class MappedObject(object):
168 def __init__(self, prototypes, usedouble = False):
170 pyargparse_chars['smpl_t'] = 'd'
171 self.prototypes = prototypes
173 self.shortname = prototypes['shortname']
174 self.longname = prototypes['longname']
175 self.new_proto = prototypes['new'][0]
176 self.del_proto = prototypes['del'][0]
177 self.do_proto = prototypes['do'][0]
178 self.input_params = get_params_types_names(self.new_proto)
179 self.input_params_list = "; ".join(get_input_params(self.new_proto))
180 self.outputs = get_params_types_names(self.do_proto)[2:]
181 self.do_inputs = [get_params_types_names(self.do_proto)[1]]
182 self.do_outputs = get_params_types_names(self.do_proto)[2:]
183 struct_output_str = ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in self.do_outputs]
184 if len(self.prototypes['rdo']):
185 rdo_outputs = get_params_types_names(prototypes['rdo'][0])[2:]
186 struct_output_str += ["PyObject *{0[name]}; {1} c_{0[name]}".format(i, i['type'][:-1]) for i in rdo_outputs]
187 self.outputs += rdo_outputs
188 self.struct_outputs = ";\n ".join(struct_output_str)
190 #print ("input_params: ", map(split_type, get_input_params(self.do_proto)))
191 #print ("output_params", map(split_type, get_output_params(self.do_proto)))
196 out += self.gen_struct()
197 out += self.gen_doc()
198 out += self.gen_new()
199 out += self.gen_init()
200 out += self.gen_del()
202 if len(self.prototypes['rdo']):
203 self.do_proto = self.prototypes['rdo'][0]
204 self.do_inputs = [get_params_types_names(self.do_proto)[1]]
205 self.do_outputs = get_params_types_names(self.do_proto)[2:]
206 out += self.gen_do(method='rdo')
207 out += self.gen_memberdef()
208 out += self.gen_set()
209 out += self.gen_get()
210 out += self.gen_methodef()
211 out += self.gen_typeobject()
212 except Exception as e:
213 print ("Failed generating code for", self.shortname)
217 def gen_struct(self):
219 // {shortname} structure
222 // pointer to aubio object
232 # fmat_t* / fvec_t* / cvec_t* inputs -> full fvec_t /.. struct in Py_{shortname}
233 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')
234 return out.format(do_inputs_list = do_inputs_list, **self.__dict__)
238 for p in self.input_params:
240 defval = aubiodefvalue[name].replace('"','\\\"')
241 sig.append("{name}={defval}".format(defval=defval, name=name))
243 #ifndef PYAUBIO_{shortname}_doc
244 #define PYAUBIO_{shortname}_doc "{shortname}({sig})"
245 #endif /* PYAUBIO_{shortname}_doc */
247 static char Py_{shortname}_doc[] = ""
248 PYAUBIO_{shortname}_doc
251 return out.format(sig=', '.join(sig), **self.__dict__)
257 Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
259 Py_{shortname} *self;
260 """.format(**self.__dict__)
261 params = self.input_params
264 {type} {name} = {defval};""".format(defval = param_init[p['type']], **p)
265 plist = ", ".join(["\"%s\"" % p['name'] for p in params])
267 static char *kwlist[] = {{ {plist}, NULL }};""".format(plist = plist)
268 argchars = "".join([pyargparse_chars[p['type']] for p in params])
269 arglist = ", ".join(["&%s" % p['name'] for p in params])
271 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|{argchars}", kwlist,
275 """.format(argchars = argchars, arglist = arglist)
277 self = (Py_{shortname} *) pytype->tp_alloc (pytype, 0);
281 """.format(**self.__dict__)
282 params = self.input_params
284 out += self.check_valid(p)
286 return (PyObject *)self;
291 def check_valid(self, p):
292 if p['type'] == 'uint_t':
293 return self.check_valid_uint(p)
294 if p['type'] == 'char_t*':
295 return self.check_valid_char(p)
296 if p['type'] == 'smpl_t':
297 return self.check_valid_smpl(p)
299 print ("ERROR, no idea how to check %s for validity" % p['type'])
301 def check_valid_uint(self, p):
304 self->{name} = {defval};
305 if ((sint_t){name} > 0) {{
306 self->{name} = {name};
307 }} else if ((sint_t){name} < 0) {{
308 PyErr_SetString (PyExc_ValueError, "can not use negative value for {name}");
311 """.format(defval = aubiodefvalue[name], name = name)
313 def check_valid_char(self, p):
316 self->{name} = {defval};
317 if ({name} != NULL) {{
318 self->{name} = {name};
320 """.format(defval = aubiodefvalue[name], name = name)
322 def check_valid_smpl(self, p):
325 self->{name} = {defval};
327 self->{name} = {name};
329 """.format(defval = aubiodefvalue[name], name = name)
335 Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds)
337 """.format(**self.__dict__)
338 new_name = get_name(self.new_proto)
339 new_params = ", ".join(["self->%s" % s['name'] for s in self.input_params])
341 self->o = {new_name}({new_params});
342 """.format(new_name = new_name, new_params = new_params)
344 paramvals = "self->method"
346 // return -1 and set error string on failure
347 if (self->o == NULL) {{
348 PyErr_Format (PyExc_RuntimeError, "failed creating {shortname}");
351 """.format(paramchars = paramchars, paramvals = paramvals, **self.__dict__)
353 for o in self.outputs:
355 self->{name} = {create_fn}({output_size});""".format(name = o['name'], create_fn = newfromtype_fn[o['type']], output_size = objoutsize[self.shortname])
357 // TODO get internal params after actual object creation?
360 // create outputs{output_create}
361 """.format(output_create = output_create)
368 def gen_memberdef(self):
370 static PyMemberDef Py_{shortname}_members[] = {{
371 """.format(**self.__dict__)
372 for p in get_params_types_names(self.new_proto):
373 tmp = " {{\"{name}\", {ttype}, offsetof (Py_{shortname}, {name}), READONLY, \"TODO documentation\"}},\n"
374 pytype = member_types[p['type']]
375 out += tmp.format(name = p['name'], ttype = pytype, shortname = self.shortname)
376 out += """ {NULL}, // sentinel
385 Py_{shortname}_del (Py_{shortname} * self, PyObject * unused)
386 {{""".format(**self.__dict__)
387 for input_param in self.do_inputs:
388 if input_param['type'] == 'fmat_t *':
390 free(self->{0[name]}.data);""".format(input_param)
391 for o in self.outputs:
393 del_out = delfromtype_fn[o['type']]
396 {del_out}(self->{name});
397 }}""".format(del_out = del_out, name = name)
398 del_fn = get_name(self.del_proto)
403 Py_TYPE(self)->tp_free((PyObject *) self);
405 """.format(del_fn = del_fn)
408 def gen_do(self, method = 'do'):
412 Pyaubio_{shortname}_{method} (Py_{shortname} * self, PyObject * args)
413 {{""".format(method = method, **self.__dict__)
414 input_params = self.do_inputs
415 output_params = self.do_outputs
419 PyObject *outputs;"""
420 for input_param in input_params:
422 PyObject *py_{0};""".format(input_param['name'])
423 refs = ", ".join(["&py_%s" % p['name'] for p in input_params])
424 pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params])
426 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
428 }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
429 for input_param in input_params:
432 if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{
434 }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
435 if self.shortname in objinputsize:
438 if (self->{0[name]}.length != {expected_size}) {{
439 PyErr_Format (PyExc_ValueError,
440 "input size of {shortname} should be %d, not %d",
441 {expected_size}, self->{0[name]}.length);
443 }}""".format(input_param, expected_size = objinputsize[self.shortname], **self.__dict__)
447 // TODO: check input sizes"""
448 for output_param in output_params:
451 Py_INCREF(self->{0[name]});
452 if (!{pytoaubio}(self->{0[name]}, &(self->c_{0[name]}))) {{
454 }}""".format(output_param, pytoaubio = pytoaubio_fn[output_param['type']])
455 do_fn = get_name(self.do_proto)
456 inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params])
457 c_outputs = ", ".join(["&(self->c_%s)" % p['name'] for p in self.do_outputs])
458 outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
461 {do_fn}(self->o, {inputs}, {c_outputs});
464 inputs = inputs, c_outputs = c_outputs,
466 if len(self.do_outputs) > 1:
468 outputs = PyTuple_New({:d});""".format(len(self.do_outputs))
469 for i, p in enumerate(self.do_outputs):
471 PyTuple_SetItem( outputs, {i}, self->{p[name]});""".format(i = i, p = p)
474 outputs = self->{p[name]};""".format(p = self.do_outputs[0])
486 // {shortname} setters
487 """.format(**self.__dict__)
488 for set_param in self.prototypes['set']:
489 params = get_params_types_names(set_param)[1:]
490 param = self.shortname.split('_set_')[-1]
491 paramdecls = "".join(["""
492 {0} {1};""".format(p['type'], p['name']) for p in params])
493 method_name = get_name(set_param)
494 param = method_name.split('aubio_'+self.shortname+'_set_')[-1]
495 refs = ", ".join(["&%s" % p['name'] for p in params])
496 paramlist = ", ".join(["%s" % p['name'] for p in params])
498 paramlist = "," + paramlist
499 pyparamtypes = ''.join([pyargparse_chars[p['type']] for p in params])
502 Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args)
506 """.format(param = param, paramdecls = paramdecls, **self.__dict__)
508 if len(refs) and len(pyparamtypes):
511 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
514 """.format(pyparamtypes = pyparamtypes, refs = refs)
517 err = aubio_{shortname}_set_{param} (self->o {paramlist});
520 if (PyErr_Occurred() == NULL) {{
521 PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}");
523 // change the RuntimeError into ValueError
524 PyObject *type, *value, *traceback;
525 PyErr_Fetch(&type, &value, &traceback);
527 type = PyExc_ValueError;
529 PyErr_Restore(type, value, traceback);
535 """.format(param = param, refs = refs, paramdecls = paramdecls,
536 pyparamtypes = pyparamtypes, paramlist = paramlist, **self.__dict__)
541 // {shortname} getters
542 """.format(**self.__dict__)
543 for method in self.prototypes['get']:
544 params = get_params_types_names(method)
545 method_name = get_name(method)
546 assert len(params) == 1, \
547 "get method has more than one parameter %s" % params
548 param = method_name.split('aubio_'+self.shortname+'_get_')[-1]
549 paramtype = get_return_type(method)
550 ptypeconv = pyfromtype_fn[paramtype]
553 Pyaubio_{shortname}_get_{param} (Py_{shortname} *self, PyObject *unused)
555 {ptype} {param} = aubio_{shortname}_get_{param} (self->o);
556 return (PyObject *){ptypeconv} ({param});
558 """.format(param = param, ptype = paramtype, ptypeconv = ptypeconv,
562 def gen_methodef(self):
564 static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__)
565 for m in self.prototypes['set']:
567 shortname = name.replace('aubio_%s_' % self.shortname, '')
569 {{"{shortname}", (PyCFunction) Py{name},
570 METH_VARARGS, ""}},""".format(name = name, shortname = shortname)
571 for m in self.prototypes['get']:
573 shortname = name.replace('aubio_%s_' % self.shortname, '')
575 {{"{shortname}", (PyCFunction) Py{name},
576 METH_NOARGS, ""}},""".format(name = name, shortname = shortname)
577 for m in self.prototypes['rdo']:
579 shortname = name.replace('aubio_%s_' % self.shortname, '')
581 {{"{shortname}", (PyCFunction) Py{name},
582 METH_VARARGS, ""}},""".format(name = name, shortname = shortname)
584 {NULL} /* sentinel */
589 def gen_typeobject(self):
591 PyTypeObject Py_{shortname}Type = {{
592 //PyObject_HEAD_INIT (NULL)
594 PyVarObject_HEAD_INIT (NULL, 0)
596 sizeof (Py_{shortname}),
598 (destructor) Py_{shortname}_del,
608 (ternaryfunc)Pyaubio_{shortname}_do,
621 Py_{shortname}_methods,
622 Py_{shortname}_members,
629 (initproc) Py_{shortname}_init,
642 """.format(**self.__dict__)