source: python/lib/gen_code.py @ 1164fd85

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

python/lib/gen_code.py: remove unused variable

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