source: python/lib/gen_code.py @ bf54364

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

python/lib/gen_code.py: add input size checks (see #63)

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