source: python/lib/gen_code.py @ 633400d

feature/cnnfeature/crepefeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 633400d was 633400d, checked in by Paul Brossier <piem@piem.org>, 5 years ago

Merge branch 'master' into feature/pitchshift

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