source: python/lib/gen_code.py @ f9cca9c

feature/cnnfeature/crepefeature/pitchshiftfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretch
Last change on this file since f9cca9c was f9cca9c, checked in by Paul Brossier <piem@piem.org>, 8 years ago

python/lib/gen_code.py: compare to value in param_init

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