2 # we have some clean up to do
3 'buf_size': 'Py_default_vector_length',
4 'win_s': 'Py_default_vector_length',
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
20 'method': '"default"',
26 'char_t*': 'T_STRING',
28 'smpl_t': 'AUBIO_NPY_SMPL',
32 'smpl_t': 'PyFloat_FromDouble',
33 'uint_t': 'PyLong_FromLong', # was: 'PyInt_FromLong',
34 'fvec_t*': 'PyAubio_CFvecToArray',
35 'fmat_t*': 'PyAubio_CFmatToArray',
39 'fvec_t*': 'PyAubio_ArrayToCFvec',
40 'cvec_t*': 'PyAubio_PyCvecToCCvec',
41 #'fmat_t*': 'PyAubio_ArrayToCFmat',
45 'fvec_t*': 'PyAubio_CFvecToArray',
46 'cvec_t*': 'PyAubio_CCvecToArray',
47 'fmat_t*': 'PyAubio_CFmatToArray',
51 'fvec_t*': 'new_py_fvec',
52 'fmat_t*': 'new_py_fmat',
53 'cvec_t*': 'new_py_cvec',
57 'fvec_t*': 'Py_DECREF',
58 'fmat_t*': 'Py_DECREF',
59 'cvec_t*': 'Py_DECREF',
71 'smpl_t': 'f', # if not usedouble else 'd',
83 'wavetable': 'self->hop_size',
84 'sampler': 'self->hop_size',
85 'mfcc': 'self->n_coeffs',
88 'filterbank': 'self->n_filters',
89 'tss': 'self->hop_size',
93 name = proto.replace(' *', '* ').split()[1].split('(')[0]
94 name = name.replace('*','')
95 if name == '': raise ValueError(proto + "gave empty name")
98 def get_return_type(proto):
100 paramregex = re.compile('(\w+ ?\*?).*')
101 outputs = paramregex.findall(proto)
102 assert len(outputs) == 1
103 return outputs[0].replace(' ', '')
106 """ arg = 'foo *name'
107 return ['foo*', 'name'] """
109 type_arg = {} #'type': l[0], 'name': l[1]}
110 type_arg['type'] = " ".join(l[:-1])
111 type_arg['name'] = l[-1]
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 ','')
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']
131 paramregex = re.compile('.*\((.*)\);')
132 a = paramregex.findall(proto)[0].split(', ')
133 #a = [i.replace('const ', '') for i in a]
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 '))]
140 def get_output_params(proto):
141 a = get_params(proto)
142 return [i for i in a if not i.startswith('const ')][1:]
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']]
149 a = list(map(split_type, get_params(proto)))
151 #import sys; sys.exit(1)
154 class MappedObject(object):
156 def __init__(self, prototypes, usedouble = False):
158 pyargparse_chars['smpl_t'] = 'd'
159 self.prototypes = prototypes
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)
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)))
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()
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()
192 def gen_struct(self):
194 // {shortname} structure
197 // pointer to aubio object
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__)
213 // TODO: add documentation
214 static char Py_{shortname}_doc[] = \"undefined\";
216 return out.format(**self.__dict__)
222 Py_{shortname}_new (PyTypeObject * pytype, PyObject * args, PyObject * kwds)
224 Py_{shortname} *self;
225 """.format(**self.__dict__)
226 params = self.input_params
229 {type} {name} = {defval};""".format(defval = param_init[p['type']], **p)
230 plist = ", ".join(["\"%s\"" % p['name'] for p in params])
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])
236 if (!PyArg_ParseTupleAndKeywords (args, kwds, "|{argchars}", kwlist,
240 """.format(argchars = argchars, arglist = arglist)
242 self = (Py_{shortname} *) pytype->tp_alloc (pytype, 0);
246 """.format(**self.__dict__)
247 params = self.input_params
249 out += self.check_valid(p)
251 return (PyObject *)self;
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)
262 print ("ERROR, no idea how to check %s for validity" % p['type'])
264 def check_valid_uint(self, p):
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}");
274 """.format(defval = aubiodefvalue[name], name = name)
276 def check_valid_char(self, p):
279 self->{name} = {defval};
280 if ({name} != NULL) {{
281 self->{name} = {name};
283 """.format(defval = aubiodefvalue[name], name = name)
289 Py_{shortname}_init (Py_{shortname} * self, PyObject * args, PyObject * kwds)
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])
295 self->o = {new_name}({new_params});
296 """.format(new_name = new_name, new_params = new_params)
298 paramvals = "self->method"
300 // return -1 and set error string on failure
301 if (self->o == NULL) {{
302 PyErr_Format (PyExc_Exception, "failed creating {shortname}");
305 """.format(paramchars = paramchars, paramvals = paramvals, **self.__dict__)
307 for o in self.outputs:
309 self->{name} = {create_fn}({output_size});""".format(name = o['name'], create_fn = newfromtype_fn[o['type']], output_size = objoutsize[self.shortname])
311 // TODO get internal params after actual object creation?
314 // create outputs{output_create}
315 """.format(output_create = output_create)
322 def gen_memberdef(self):
324 static PyMemberDef Py_{shortname}_members[] = {{
325 """.format(**self.__dict__)
326 for p in get_params_types_names(self.new_proto):
327 tmp = " {{\"{name}\", {ttype}, offsetof (Py_{shortname}, {name}), READONLY, \"TODO documentation\"}},\n"
328 pytype = member_types[p['type']]
329 out += tmp.format(name = p['name'], ttype = pytype, shortname = self.shortname)
330 out += """ {NULL}, // sentinel
339 Py_{shortname}_del (Py_{shortname} * self, PyObject * unused)
340 {{""".format(**self.__dict__)
341 for input_param in self.do_inputs:
342 if input_param['type'] == 'fmat_t *':
344 free(self->{0[name]}.data);""".format(input_param)
345 for o in self.outputs:
347 del_out = delfromtype_fn[o['type']]
349 {del_out}(self->{name});""".format(del_out = del_out, name = name)
350 del_fn = get_name(self.del_proto)
355 Py_TYPE(self)->tp_free((PyObject *) self);
357 """.format(del_fn = del_fn)
364 Py_{shortname}_do (Py_{shortname} * self, PyObject * args)
365 {{""".format(**self.__dict__)
366 input_params = self.do_inputs
367 output_params = self.do_outputs
370 for input_param in input_params:
372 PyObject *py_{0};""".format(input_param['name'])
373 refs = ", ".join(["&py_%s" % p['name'] for p in input_params])
374 pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params])
376 if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
378 }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
379 for input_param in input_params:
382 if (!{pytoaubio}(py_{0[name]}, &(self->{0[name]}))) {{
384 }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
387 // TODO: check input sizes"""
388 for output_param in output_params:
391 Py_INCREF(self->{0[name]});
392 if (!{pytoaubio}(self->{0[name]}, &(self->c_{0[name]}))) {{
394 }}""".format(output_param, pytoaubio = pytoaubio_fn[output_param['type']])
395 do_fn = get_name(self.do_proto)
396 inputs = ", ".join(['&(self->'+p['name']+')' for p in input_params])
397 c_outputs = ", ".join(["&(self->c_%s)" % p['name'] for p in self.do_outputs])
398 outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
401 {do_fn}(self->o, {inputs}, {c_outputs});
407 inputs = inputs, outputs = outputs, c_outputs = c_outputs,
413 // {shortname} setters
414 """.format(**self.__dict__)
415 for set_param in self.prototypes['set']:
416 params = get_params_types_names(set_param)[1]
417 paramtype = params['type']
418 method_name = get_name(set_param)
419 param = method_name.split('aubio_'+self.shortname+'_set_')[-1]
420 pyparamtype = pyargparse_chars[paramtype]
423 Pyaubio_{shortname}_set_{param} (Py_{shortname} *self, PyObject *args)
428 if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{
431 err = aubio_{shortname}_set_{param} (self->o, {param});
434 PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}");
439 """.format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__)
444 // {shortname} getters
445 """.format(**self.__dict__)
446 for method in self.prototypes['get']:
447 params = get_params_types_names(method)
448 method_name = get_name(method)
449 assert len(params) == 1, \
450 "get method has more than one parameter %s" % params
451 param = method_name.split('aubio_'+self.shortname+'_get_')[-1]
452 paramtype = get_return_type(method)
453 ptypeconv = pyfromtype_fn[paramtype]
456 Pyaubio_{shortname}_get_{param} (Py_{shortname} *self, PyObject *unused)
458 {ptype} {param} = aubio_{shortname}_get_{param} (self->o);
459 return (PyObject *){ptypeconv} ({param});
461 """.format(param = param, ptype = paramtype, ptypeconv = ptypeconv,
465 def gen_methodef(self):
467 static PyMethodDef Py_{shortname}_methods[] = {{""".format(**self.__dict__)
468 for m in self.prototypes['set']:
470 shortname = name.replace('aubio_%s_' % self.shortname, '')
472 {{"{shortname}", (PyCFunction) Py{name},
473 METH_VARARGS, ""}},""".format(name = name, shortname = shortname)
474 for m in self.prototypes['get']:
476 shortname = name.replace('aubio_%s_' % self.shortname, '')
478 {{"{shortname}", (PyCFunction) Py{name},
479 METH_NOARGS, ""}},""".format(name = name, shortname = shortname)
481 {NULL} /* sentinel */
486 def gen_typeobject(self):
488 PyTypeObject Py_{shortname}Type = {{
489 //PyObject_HEAD_INIT (NULL)
491 PyVarObject_HEAD_INIT (NULL, 0)
493 sizeof (Py_{shortname}),
495 (destructor) Py_{shortname}_del,
505 (ternaryfunc)Py_{shortname}_do,
518 Py_{shortname}_methods,
519 Py_{shortname}_members,
526 (initproc) Py_{shortname}_init,
539 """.format(**self.__dict__)