source: python/lib/gen_code.py @ 3d650a7

sampler
Last change on this file since 3d650a7 was 3d650a7, checked in by Paul Brossier <piem@piem.org>, 7 years ago

python/lib/gen_code.py: show which failed before raising exception

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