source: python/lib/gen_code.py @ 45c2c5c

feature/constantq
Last change on this file since 45c2c5c was 6203a70, checked in by Paul Brossier <piem@piem.org>, 6 years ago

Merge branch 'master' into feature/constantq

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