Changeset a7f398d


Ignore:
Timestamp:
Apr 21, 2016, 9:32:59 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
e31aad20
Parents:
b5bef11
Message:

python/lib/gen_code.py: use new proxy functions

Location:
python/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/lib/gen_code.py

    rb5bef11 ra7f398d  
    3939        'fvec_t*': 'PyAubio_ArrayToCFvec',
    4040        'cvec_t*': 'PyAubio_ArrayToCCvec',
    41         'fmat_t*': 'PyAubio_ArrayToCFmat',
     41        #'fmat_t*': 'PyAubio_ArrayToCFmat',
    4242        }
    4343
     
    7575        'fmat_t*': 'O',
    7676        'fvec_t*': 'O',
     77        'cvec_t*': 'O',
    7778        }
    7879
     
    8485        'mfcc': 'self->n_coeffs',
    8586        'specdesc': '1',
    86         'tempo': '1',
     87        'tempo': '2',
    8788        'filterbank': 'self->n_filters',
     89        'tss': 'self->hop_size',
    8890        }
    8991
     
    105107        return ['foo*', 'name'] """
    106108    l = arg.split()
    107     type_arg = {'type': l[0], 'name': l[1]}
    108     # ['foo', '*name'] -> ['foo*', 'name']
    109     if l[-1].startswith('*'):
    110         #return [l[0]+'*', l[1][1:]]
    111         type_arg['type'] = l[0] + '*'
    112         type_arg['name'] = l[1][1:]
    113     # ['foo', '*', 'name'] -> ['foo*', 'name']
    114     if len(l) == 3:
    115         #return [l[0]+l[1], l[2]]
    116         type_arg['type'] = l[0]+l[1]
    117         type_arg['name'] = l[2]
    118     else:
    119         #return l
    120         pass
     109    type_arg = {} #'type': l[0], 'name': l[1]}
     110    type_arg['type'] = " ".join(l[:-1])
     111    type_arg['name'] = l[-1]
     112    # fix up type / name
     113    if type_arg['name'].startswith('*'):
     114        # ['foo', '*name'] -> ['foo*', 'name']
     115        type_arg['type'] += '*'
     116        type_arg['name'] = type_arg['name'][1:]
     117    if type_arg['type'].endswith(' *'):
     118        # ['foo *', 'name'] -> ['foo*', 'name']
     119        type_arg['type'] = type_arg['type'].replace(' *','*')
     120    if type_arg['type'].startswith('const '):
     121        # ['foo *', 'name'] -> ['foo*', 'name']
     122        type_arg['type'] = type_arg['type'].replace('const ','')
    121123    return type_arg
    122124
     
    127129    """
    128130    import re
    129     paramregex = re.compile('[\(, ](\w+ \*?\*? ?\w+)[, \)]')
    130     return paramregex.findall(proto)
     131    paramregex = re.compile('.*\((.*)\);')
     132    a = paramregex.findall(proto)[0].split(', ')
     133    #a = [i.replace('const ', '') for i in a]
     134    return a
     135
     136def get_input_params(proto):
     137    a = get_params(proto)
     138    return [i.replace('const ', '') for i in a if (i.startswith('const ') or i.startswith('uint_t ') or i.startswith('smpl_t '))]
     139
     140def get_output_params(proto):
     141    a = get_params(proto)
     142    return [i for i in a if not i.startswith('const ')][1:]
    131143
    132144def get_params_types_names(proto):
     
    135147    returns: [['int', 'argc'], ['char **','argv']]
    136148    """
    137     return list(map(split_type, get_params(proto)))
    138 
     149    a = list(map(split_type, get_params(proto)))
     150    #print proto, a
     151    #import sys; sys.exit(1)
     152    return a
    139153
    140154class MappedObject(object):
     
    149163        self.do_proto = prototypes['do'][0]
    150164        self.input_params = get_params_types_names(self.new_proto)
    151         self.input_params_list = "; ".join(get_params(self.new_proto))
     165        self.input_params_list = "; ".join(get_input_params(self.new_proto))
    152166        self.outputs = get_params_types_names(self.do_proto)[2:]
    153         self.outputs_flat = get_params(self.do_proto)[2:]
    154         self.output_results = ", ".join(self.outputs_flat)
     167        self.do_inputs = [get_params_types_names(self.do_proto)[1]]
     168        self.do_outputs = get_params_types_names(self.do_proto)[2:]
     169        self.outputs_flat = get_output_params(self.do_proto)
     170        self.output_results = "; ".join(self.outputs_flat)
     171
     172        print "input_params", map(split_type, get_input_params(self.do_proto))
     173        print "output_params", map(split_type, get_output_params(self.do_proto))
    155174
    156175    def gen_code(self):
     
    178197    // input parameters
    179198    {input_params_list};
     199    // do input vectors
     200    {do_inputs_list};
    180201    // output results
    181202    {output_results};
    182203}} Py_{shortname};
    183204"""
    184         return out.format(**self.__dict__)
     205        return out.format(do_inputs_list = "; ".join(get_input_params(self.do_proto)), **self.__dict__)
    185206
    186207    def gen_doc(self):
     
    188209// TODO: add documentation
    189210static char Py_{shortname}_doc[] = \"undefined\";
    190     """
     211"""
    191212        return out.format(**self.__dict__)
    192213
     
    222243        params = self.input_params
    223244        for p in params:
    224             out += self.check_valid(p) 
     245            out += self.check_valid(p)
    225246        out += """
    226247    return (PyObject *)self;
     
    290311  // TODO get internal params after actual object creation?
    291312"""
     313        for input_param in self.do_inputs:
     314            out += """
     315  self->{0} = ({1})malloc(sizeof({2}));""".format(input_param['name'], input_param['type'], input_param['type'][:-1])
    292316        out += """
    293317  // create outputs{output_create}
     
    318342Py_{shortname}_del  (Py_{shortname} * self, PyObject * unused)
    319343{{""".format(**self.__dict__)
     344        for input_param in self.do_inputs:
     345            out += """
     346    free(self->{0[name]});""".format(input_param)
    320347        for o in self.outputs:
    321348            name = o['name']
     
    332359
    333360    def gen_do(self):
    334         do_fn = get_name(self.do_proto)
    335         input_param = get_params_types_names(self.do_proto)[1];
    336         pytoaubio = pytoaubio_fn[input_param['type']]
    337361        output = self.outputs[0]
    338362        out = """
     
    340364static PyObject*
    341365Py_{shortname}_do  (Py_{shortname} * self, PyObject * args)
    342 {{
    343     PyObject * in_obj;
    344     {input_type} {input_name};
    345 
    346     if (!PyArg_ParseTuple (args, "O", &in_obj)) {{
     366{{""".format(**self.__dict__)
     367        input_params = self.do_inputs
     368        output_params = self.do_outputs
     369        #print input_params
     370        #print output_params
     371        for input_param in input_params:
     372            out += """
     373    PyObject *py_{0};""".format(input_param['name'], input_param['type'])
     374        refs = ", ".join(["&py_%s" % p['name'] for p in input_params])
     375        pyparamtypes = "".join([pyargparse_chars[p['type']] for p in input_params])
     376        out += """
     377    if (!PyArg_ParseTuple (args, "{pyparamtypes}", {refs})) {{
    347378        return NULL;
    348     }}
    349     {input_name} = {pytoaubio} (in_obj);
    350     if ({input_name} == NULL) {{
     379    }}""".format(refs = refs, pyparamtypes = pyparamtypes, **self.__dict__)
     380        for p in input_params:
     381            out += """
     382    if (!{pytoaubio}(py_{0[name]}, self->{0[name]})) {{
    351383        return NULL;
    352     }}
    353 
    354     {do_fn}(self->o, {input_name}, {outputs});
     384    }}""".format(input_param, pytoaubio = pytoaubio_fn[input_param['type']])
     385        do_fn = get_name(self.do_proto)
     386        inputs = ", ".join(['self->'+p['name'] for p in input_params])
     387        outputs = ", ".join(["self->%s" % p['name'] for p in self.do_outputs])
     388        out += """
     389
     390    {do_fn}(self->o, {inputs}, {outputs});
    355391
    356392    return (PyObject *) {aubiotonumpy} ({outputs});
    357393}}
    358 """
    359         return out.format(do_fn = do_fn,
    360                 shortname = self.prototypes['shortname'],
    361                 input_name = input_param['name'],
    362                 input_type= input_param['type'],
    363                 pytoaubio = pytoaubio,
    364                 outputs = ", ".join(["self->%s" % p['name'] for p in self.outputs]),
    365                 aubiotonumpy = pyfromaubio_fn[output['type']],
    366                 )
     394""".format(
     395        do_fn = do_fn,
     396        aubiotonumpy = pyfromaubio_fn[output['type']],
     397        inputs = inputs, outputs = outputs,
     398        )
     399        return out
    367400
    368401    def gen_set(self):
  • python/lib/gen_external.py

    rb5bef11 ra7f398d  
    117117            elif len(lib[o][family]) == 1:
    118118                print ( "{:15s} {:10s} {:s}".format(o, family, lib[o][family][0] ) )
    119             else:                                             
     119            else:
    120120                print ( "{:15s} {:10s} {:d}".format(o, family, len(lib[o][family]) ) )
    121121    """
     
    159159        print ("wrote %s" % output_file )
    160160        sources_list.append(output_file)
    161    
     161
    162162    objlist = "".join(["extern PyTypeObject Py_%sType;\n" % p for p in lib])
    163163    out = """
Note: See TracChangeset for help on using the changeset viewer.