Changeset a7f398d for python/lib/gen_code.py
- Timestamp:
- Apr 21, 2016, 9:32:59 PM (9 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/lib/gen_code.py
rb5bef11 ra7f398d 39 39 'fvec_t*': 'PyAubio_ArrayToCFvec', 40 40 'cvec_t*': 'PyAubio_ArrayToCCvec', 41 'fmat_t*': 'PyAubio_ArrayToCFmat',41 #'fmat_t*': 'PyAubio_ArrayToCFmat', 42 42 } 43 43 … … 75 75 'fmat_t*': 'O', 76 76 'fvec_t*': 'O', 77 'cvec_t*': 'O', 77 78 } 78 79 … … 84 85 'mfcc': 'self->n_coeffs', 85 86 'specdesc': '1', 86 'tempo': ' 1',87 'tempo': '2', 87 88 'filterbank': 'self->n_filters', 89 'tss': 'self->hop_size', 88 90 } 89 91 … … 105 107 return ['foo*', 'name'] """ 106 108 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 l120 pass109 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 ','') 121 123 return type_arg 122 124 … … 127 129 """ 128 130 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 136 def 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 140 def get_output_params(proto): 141 a = get_params(proto) 142 return [i for i in a if not i.startswith('const ')][1:] 131 143 132 144 def get_params_types_names(proto): … … 135 147 returns: [['int', 'argc'], ['char **','argv']] 136 148 """ 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 139 153 140 154 class MappedObject(object): … … 149 163 self.do_proto = prototypes['do'][0] 150 164 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)) 152 166 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)) 155 174 156 175 def gen_code(self): … … 178 197 // input parameters 179 198 {input_params_list}; 199 // do input vectors 200 {do_inputs_list}; 180 201 // output results 181 202 {output_results}; 182 203 }} Py_{shortname}; 183 204 """ 184 return out.format( **self.__dict__)205 return out.format(do_inputs_list = "; ".join(get_input_params(self.do_proto)), **self.__dict__) 185 206 186 207 def gen_doc(self): … … 188 209 // TODO: add documentation 189 210 static char Py_{shortname}_doc[] = \"undefined\"; 190 211 """ 191 212 return out.format(**self.__dict__) 192 213 … … 222 243 params = self.input_params 223 244 for p in params: 224 out += self.check_valid(p) 245 out += self.check_valid(p) 225 246 out += """ 226 247 return (PyObject *)self; … … 290 311 // TODO get internal params after actual object creation? 291 312 """ 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]) 292 316 out += """ 293 317 // create outputs{output_create} … … 318 342 Py_{shortname}_del (Py_{shortname} * self, PyObject * unused) 319 343 {{""".format(**self.__dict__) 344 for input_param in self.do_inputs: 345 out += """ 346 free(self->{0[name]});""".format(input_param) 320 347 for o in self.outputs: 321 348 name = o['name'] … … 332 359 333 360 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']]337 361 output = self.outputs[0] 338 362 out = """ … … 340 364 static PyObject* 341 365 Py_{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})) {{ 347 378 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]})) {{ 351 383 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}); 355 391 356 392 return (PyObject *) {aubiotonumpy} ({outputs}); 357 393 }} 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 367 400 368 401 def gen_set(self):
Note: See TracChangeset
for help on using the changeset viewer.