Ignore:
Timestamp:
Dec 5, 2018, 10:34:39 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/cnn, feature/crepe, feature/pitchshift, feature/timestretch, fix/ffmpeg5, master
Children:
283a619a
Parents:
5b46bc3 (diff), f19db54 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/pitchshift

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/gen_code.py

    r5b46bc3 r633400d  
    33    'buf_size': 'Py_default_vector_length',
    44    'win_s': 'Py_default_vector_length',
     5    'size': 'Py_default_vector_length',
    56    # and here too
    67    'hop_size': 'Py_default_vector_length / 2',
     
    8586        'tss': 'self->buf_size',
    8687        'pitchshift': 'self->hop_size',
     88        'dct': 'self->size',
    8789        }
    8890
     
    180182        self.do_outputs = get_params_types_names(self.do_proto)[2:]
    181183        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
    182188        self.struct_outputs = ";\n    ".join(struct_output_str)
    183189
     
    187193    def gen_code(self):
    188194        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()
     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
    200215        return out
    201216
     
    381396        return out
    382397
    383     def gen_do(self):
     398    def gen_do(self, method = 'do'):
    384399        out = """
    385400// do {shortname}
    386401static PyObject*
    387 Py_{shortname}_do  (Py_{shortname} * self, PyObject * args)
    388 {{""".format(**self.__dict__)
     402Pyaubio_{shortname}_{method}  (Py_{shortname} * self, PyObject * args)
     403{{""".format(method = method, **self.__dict__)
    389404        input_params = self.do_inputs
    390405        output_params = self.do_outputs
     
    462477""".format(**self.__dict__)
    463478        for set_param in self.prototypes['set']:
    464             params = get_params_types_names(set_param)[1]
    465             paramtype = params['type']
     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])
    466483            method_name = get_name(set_param)
    467484            param = method_name.split('aubio_'+self.shortname+'_set_')[-1]
    468             pyparamtype = pyargparse_chars[paramtype]
     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])
    469490            out += """
    470491static PyObject *
     
    472493{{
    473494  uint_t err = 0;
    474   {paramtype} {param};
    475 
    476   if (!PyArg_ParseTuple (args, "{pyparamtype}", &{param})) {{
     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})) {{
    477502    return NULL;
    478503  }}
    479   err = aubio_{shortname}_set_{param} (self->o, {param});
     504""".format(pyparamtypes = pyparamtypes, refs = refs)
     505
     506            out += """
     507  err = aubio_{shortname}_set_{param} (self->o {paramlist});
    480508
    481509  if (err > 0) {{
    482     PyErr_SetString (PyExc_ValueError, "error running aubio_{shortname}_set_{param}");
     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    }}
    483518    return NULL;
    484519  }}
    485520  Py_RETURN_NONE;
    486521}}
    487 """.format(param = param, paramtype = paramtype, pyparamtype = pyparamtype, **self.__dict__)
     522""".format(param = param, refs = refs, paramdecls = paramdecls,
     523        pyparamtypes = pyparamtypes, paramlist = paramlist, **self.__dict__)
    488524        return out
    489525
     
    526562  {{"{shortname}", (PyCFunction) Py{name},
    527563    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)
    528570        out += """
    529571  {NULL} /* sentinel */
     
    551593  0,
    552594  0,
    553   (ternaryfunc)Py_{shortname}_do,
     595  (ternaryfunc)Pyaubio_{shortname}_do,
    554596  0,
    555597  0,
Note: See TracChangeset for help on using the changeset viewer.