source: python/lib/gen_code.py @ 73e068d

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 73e068d was 73e068d, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/lib/gen_code.py: add support for multiple _do outputs

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